#include "ManagedOdePackProvider.h"

/* Common Block Declarations */

union
{
  struct
  {
    doublereal rowns[209], ccmax, el0, h__, hmin, hmxi, hu, rc, tn, uround;
    integer iownd[6], iowns[6], icf, ierpj, iersl, jcur, jstart, kflag, l, lyh,
      lewt, lacor, lsavf, lwm, liwm, meth, miter, maxord, maxcor, msbp, mxncf,
      n, nq, nst, nfe, nje, nqu;
  } _1;
  struct
  {
    doublereal rls[218];
    integer ils[37];
  } _2;
  struct
  {
    doublereal conit, crate, el[13], elco[156] /* was [13][12] */, hold, rmax,
      tesco[36] /* was [3][12] */, ccmax, el0, h__, hmin, hmxi, hu, rc, tn,
      uround;
    integer iownd[6], ialth, ipup, lmax, meo, nqnyh, nslp, icf, ierpj, iersl,
      jcur, jstart, kflag, l, lyh, lewt, lacor, lsavf, lwm, liwm, meth, miter,
      maxord, maxcor, msbp, mxncf, n, nq, nst, nfe, nje, nqu;
  } _3;
}

dls001_;

#define dls001_1 (dls001_._1)
#define dls001_2 (dls001_._2)
#define dls001_3 (dls001_._3)

union
{
  struct
  {
    doublereal rlss[6];
    integer iplost, iesp, istatc, iys, iba, ibian, ibjan, ibjgp, ipian, ipjan,
      ipjgp, ipigp, ipr, ipc, ipic, ipisp, iprsp, ipa, lenyh, lenyhm, lenwk,
      lreq, lrat, lrest, lwmin, moss, msbj, nslj, ngp, nlu, nnz, nsp, nzl, nzu;
  } _1;
  struct
  {
    doublereal con0, conmin, ccmxj, psmall, rbig, seth;
    integer iplost, iesp, istatc, iys, iba, ibian, ibjan, ibjgp, ipian, ipjan,
      ipjgp, ipigp, ipr, ipc, ipic, ipisp, iprsp, ipa, lenyh, lenyhm, lenwk,
      lreq, lrat, lrest, lwmin, moss, msbj, nslj, ngp, nlu, nnz, nsp, nzl, nzu;
  } _2;
  struct
  {
    doublereal rlss[6];
    integer ilss[34];
  } _3;
}

dlss01_;

#define dlss01_1 (dlss01_._1)
#define dlss01_2 (dlss01_._2)
#define dlss01_3 (dlss01_._3)

union
{
  struct
  {
    doublereal rownd2, cm1[12], cm2[5], pdest, pdlast, ratio, pdnorm;
    integer iownd2[3], icount, irflag, jtyp, mused, mxordn, mxords;
  } _1;
  struct
  {
    doublereal rownd2, rowns2[20], pdnorm;
    integer iownd2[3], iowns2[2], jtyp, mused, mxordn, mxords;
  } _2;
  struct
  {
    doublereal rlsa[22];
    integer ilsa[9];
  } _3;
}

dlsa01_;

#define dlsa01_1 (dlsa01_._1)
#define dlsa01_2 (dlsa01_._2)
#define dlsa01_3 (dlsa01_._3)

union
{
  struct
  {
    doublereal rownr3[2], t0, tlast, toutc;
    integer iownd3[3], iownr3[2], irfnd, itaskc, ngc, nge;
  } _1;
  struct
  {
    doublereal alpha, x2, rdum3[3];
    integer iownd3[3], imax, last, idum3[4];
  } _2;
  struct
  {
    doublereal rlsr[5];
    integer ilsr[9];
  } _3;
}

dlsr01_;

#define dlsr01_1 (dlsr01_._1)
#define dlsr01_2 (dlsr01_._2)
#define dlsr01_3 (dlsr01_._3)

union
{
  struct
  {
    doublereal delt, epcon, sqrtn, rsqrtn;
    integer jpre, jacflg, locwp, lociwp, lsavx, kmp, maxl, mnewt, nni, nli, nps,
      ncfn, ncfl;
  } _1;
  struct
  {
    doublereal rlsp[4];
    integer ilsp[13];
  } _2;
}

dlpk01_;

#define dlpk01_1 (dlpk01_._1)
#define dlpk01_2 (dlpk01_._2)

union
{
  struct
  {
    doublereal stifr;
    integer newt, nsfi, nslj, njev;
  } _1;
  struct
  {
    doublereal rls2;
    integer ils2[4];
  } _2;
}

dls002_;

#define dls002_1 (dls002_._1)
#define dls002_2 (dls002_._2)

/* Table of constant values */

static doublereal c_b3 = 1.;
static integer c__30 = 30;
static integer c__51 = 51;
static integer c__0 = 0;
static integer c__1 = 1;
static doublereal c_b34 = 0.;
static integer c__52 = 52;
static integer c__60 = 60;
static integer c__2 = 2;
static integer c__5 = 5;
static integer c__4 = 4;
static integer c__3 = 3;

/* DECK DUMACH */
doublereal SmartMathLibrary::OdePack::ManagedOdePackProvider::dumach_()
{
  /* System generated locals */
  doublereal ret_val;

  /* Local variables */
  static doublereal u, comp;

  /* ***BEGIN PROLOGUE  DUMACH */
  /* ***PURPOSE  Compute the unit roundoff of the machine. */
  /* ***CATEGORY  R1 */
  /* ***TYPE      DOUBLE PRECISION (RUMACH-S, DUMACH-D) */
  /* ***KEYWORDS  MACHINE CONSTANTS */
  /* ***AUTHOR  Hindmarsh, Alan C., (LLNL) */
  /* ***DESCRIPTION */
  /* *Usage: */
  /*        DOUBLE PRECISION  A, DUMACH */
  /*        A = DUMACH() */

  /* *Function Return Values: */
  /*     A : the unit roundoff of the machine. */

  /* *Description: */
  /*     The unit roundoff is defined as the smallest positive machine */
  /*     number u such that  1.0 + u .ne. 1.0.  This is computed by DUMACH */
  /*     in a machine-independent manner. */

  /* ***REFERENCES  (NONE) */
  /* ***ROUTINES CALLED  DUMSUM */
  /* ***REVISION HISTORY  (YYYYMMDD) */
  /*   19930216  DATE WRITTEN */
  /*   19930818  Added SLATEC-format prologue.  (FNF) */
  /*   20030707  Added DUMSUM to force normal storage of COMP.  (ACH) */
  /* ***END PROLOGUE  DUMACH */

  /* ***FIRST EXECUTABLE STATEMENT  DUMACH */
  u = 1.;
  L10: u *= .5;
  dumsum_(&c_b3, &u, &comp);
  if (comp != 1.)
  {
    goto L10;
  }
  ret_val = u * 2.;
  return ret_val;
  /* ----------------------- End of Function DUMACH ------------------------ */
} /* dumach_ */

 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dumsum_
   (doublereal *a, doublereal *b, doublereal *c__)
{
  /*     Routine to force normal storing of A + B, for DUMACH. */
  *c__ =  *a +  *b;
  return 0;
} /* dumsum_ */

/* DECK DCFODE */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dcfode_
   (integer *meth, doublereal *elco, doublereal *tesco)
{
  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__, ib;
  static doublereal pc[12];
  static integer nq;
  static doublereal fnq;
  static integer nqm1, nqp1;
  static doublereal ragq, pint, xpin, fnqm1, agamq, rqfac, tsign, rq1fac;

  /* ***BEGIN PROLOGUE  DCFODE */
  /* ***SUBSIDIARY */
  /* ***PURPOSE  Set ODE integrator coefficients. */
  /* ***TYPE      DOUBLE PRECISION (SCFODE-S, DCFODE-D) */
  /* ***AUTHOR  Hindmarsh, Alan C., (LLNL) */
  /* ***DESCRIPTION */

  /*  DCFODE is called by the integrator routine to set coefficients */
  /*  needed there.  The coefficients for the current method, as */
  /*  given by the value of METH, are set for all orders and saved. */
  /*  The maximum order assumed here is 12 if METH = 1 and 5 if METH = 2. */
  /*  (A smaller value of the maximum order is also allowed.) */
  /*  DCFODE is called once at the beginning of the problem, */
  /*  and is not called again unless and until METH is changed. */

  /*  The ELCO array contains the basic method coefficients. */
  /*  The coefficients el(i), 1 .le. i .le. nq+1, for the method of */
  /*  order nq are stored in ELCO(i,nq).  They are given by a genetrating */
  /*  polynomial, i.e., */
  /*      l(x) = el(1) + el(2)*x + ... + el(nq+1)*x**nq. */
  /*  For the implicit Adams methods, l(x) is given by */
  /*      dl/dx = (x+1)*(x+2)*...*(x+nq-1)/factorial(nq-1),    l(-1) = 0. */
  /*  For the BDF methods, l(x) is given by */
  /*      l(x) = (x+1)*(x+2)* ... *(x+nq)/K, */
  /*  where         K = factorial(nq)*(1 + 1/2 + ... + 1/nq). */

  /*  The TESCO array contains test constants used for the */
  /*  local error test and the selection of step size and/or order. */
  /*  At order nq, TESCO(k,nq) is used for the selection of step */
  /*  size at order nq - 1 if k = 1, at order nq if k = 2, and at order */
  /*  nq + 1 if k = 3. */

  /* ***SEE ALSO  DLSODE */
  /* ***ROUTINES CALLED  (NONE) */
  /* ***REVISION HISTORY  (YYMMDD) */
  /*   791129  DATE WRITTEN */
  /*   890501  Modified prologue to SLATEC/LDOC format.  (FNF) */
  /*   890503  Minor cosmetic changes.  (FNF) */
  /*   930809  Renamed to allow single/double precision versions. (ACH) */
  /* ***END PROLOGUE  DCFODE */
  /* **End */

  /* ***FIRST EXECUTABLE STATEMENT  DCFODE */
  /* Parameter adjustments */
  tesco -= 4;
  elco -= 14;

  /* Function Body */
  switch (*meth)
  {
    case 1:
      goto L100;
    case 2:
      goto L200;
  }

  L100: elco[14] = 1.;
  elco[15] = 1.;
  tesco[4] = 0.;
  tesco[5] = 2.;
  tesco[7] = 1.;
  tesco[39] = 0.;
  pc[0] = 1.;
  rqfac = 1.;
  for (nq = 2; nq <= 12; ++nq)
  {
    /* ----------------------------------------------------------------------- */
    /* The PC array will contain the coefficients of the polynomial */
    /*     p(x) = (x+1)*(x+2)*...*(x+nq-1). */
    /* Initially, p(x) = 1. */
    /* ----------------------------------------------------------------------- */
    rq1fac = rqfac;
    rqfac /= nq;
    nqm1 = nq - 1;
    fnqm1 = (doublereal)nqm1;
    nqp1 = nq + 1;
    /* Form coefficients of p(x)*(x+nq-1). ---------------------------------- */
    pc[nq - 1] = 0.;
    i__1 = nqm1;
    for (ib = 1; ib <= i__1; ++ib)
    {
      i__ = nqp1 - ib;
      /* L110: */
      pc[i__ - 1] = pc[i__ - 2] + fnqm1 * pc[i__ - 1];
    }
    pc[0] = fnqm1 * pc[0];
    /* Compute integral, -1 to 0, of p(x) and x*p(x). ----------------------- */
    pint = pc[0];
    xpin = pc[0] / 2.;
    tsign = 1.;
    i__1 = nq;
    for (i__ = 2; i__ <= i__1; ++i__)
    {
      tsign =  - tsign;
      pint += tsign * pc[i__ - 1] / i__;
      /* L120: */
      xpin += tsign * pc[i__ - 1] / (i__ + 1);
    }
    /* Store coefficients in ELCO and TESCO. -------------------------------- */
    elco[nq *13+1] = pint * rq1fac;
    elco[nq *13+2] = 1.;
    i__1 = nq;
    for (i__ = 2; i__ <= i__1; ++i__)
    {
      /* L130: */
      elco[i__ + 1+nq * 13] = rq1fac * pc[i__ - 1] / i__;
    }
    agamq = rqfac * xpin;
    ragq = 1. / agamq;
    tesco[nq *3+2] = ragq;
    if (nq < 12)
    {
      tesco[nqp1 *3+1] = ragq * rqfac / nqp1;
    }
    tesco[nqm1 *3+3] = ragq;
    /* L140: */
  }
  return 0;

  L200: pc[0] = 1.;
  rq1fac = 1.;
  for (nq = 1; nq <= 5; ++nq)
  {
    /* ----------------------------------------------------------------------- */
    /* The PC array will contain the coefficients of the polynomial */
    /*     p(x) = (x+1)*(x+2)*...*(x+nq). */
    /* Initially, p(x) = 1. */
    /* ----------------------------------------------------------------------- */
    fnq = (doublereal)nq;
    nqp1 = nq + 1;
    /* Form coefficients of p(x)*(x+nq). ------------------------------------ */
    pc[nqp1 - 1] = 0.;
    i__1 = nq;
    for (ib = 1; ib <= i__1; ++ib)
    {
      i__ = nq + 2-ib;
      /* L210: */
      pc[i__ - 1] = pc[i__ - 2] + fnq * pc[i__ - 1];
    }
    pc[0] = fnq * pc[0];
    /* Store coefficients in ELCO and TESCO. -------------------------------- */
    i__1 = nqp1;
    for (i__ = 1; i__ <= i__1; ++i__)
    {
      /* L220: */
      elco[i__ + nq * 13] = pc[i__ - 1] / pc[1];
    }
    elco[nq *13+2] = 1.;
    tesco[nq *3+1] = rq1fac;
    tesco[nq *3+2] = nqp1 / elco[nq *13+1];
    tesco[nq *3+3] = (nq + 2) / elco[nq *13+1];
    rq1fac /= fnq;
    /* L230: */
  }
  return 0;
  /* ----------------------- END OF SUBROUTINE DCFODE ---------------------- */
} /* dcfode_ */

/* DECK DINTDY */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dintdy_
   (doublereal *t, integer *k, doublereal *yh, integer *nyh, doublereal *dky,
   integer *iflag)
{
  /* System generated locals */
  integer yh_dim1, yh_offset, i__1, i__2;
  doublereal d__1;

  /* Local variables */
  static doublereal c__;
  static integer i__, j;
  static doublereal r__, s;
  static integer ic, jb, jj;
  static doublereal tp;
  static integer jb2, jj1, jp1;
  static char msg[80];

  /* ***BEGIN PROLOGUE  DINTDY */
  /* ***SUBSIDIARY */
  /* ***PURPOSE  Interpolate solution derivatives. */
  /* ***TYPE      DOUBLE PRECISION (SINTDY-S, DINTDY-D) */
  /* ***AUTHOR  Hindmarsh, Alan C., (LLNL) */
  /* ***DESCRIPTION */

  /*  DINTDY computes interpolated values of the K-th derivative of the */
  /*  dependent variable vector y, and stores it in DKY.  This routine */
  /*  is called within the package with K = 0 and T = TOUT, but may */
  /*  also be called by the user for any K up to the current order. */
  /*  (See detailed instructions in the usage documentation.) */

  /*  The computed values in DKY are gotten by interpolation using the */
  /*  Nordsieck history array YH.  This array corresponds uniquely to a */
  /*  vector-valued polynomial of degree NQCUR or less, and DKY is set */
  /*  to the K-th derivative of this polynomial at T. */
  /*  The formula for DKY is: */
  /*               q */
  /*   DKY(i)  =  sum  c(j,K) * (T - tn)**(j-K) * h**(-j) * YH(i,j+1) */
  /*              j=K */
  /*  where  c(j,K) = j*(j-1)*...*(j-K+1), q = NQCUR, tn = TCUR, h = HCUR. */
  /*  The quantities  nq = NQCUR, l = nq+1, N = NEQ, tn, and h are */
  /*  communicated by COMMON.  The above sum is done in reverse order. */
  /*  IFLAG is returned negative if either K or T is out of bounds. */

  /* ***SEE ALSO  DLSODE */
  /* ***ROUTINES CALLED  XERRWD */
  /* ***COMMON BLOCKS    DLS001 */
  /* ***REVISION HISTORY  (YYMMDD) */
  /*   791129  DATE WRITTEN */
  /*   890501  Modified prologue to SLATEC/LDOC format.  (FNF) */
  /*   890503  Minor cosmetic changes.  (FNF) */
  /*   930809  Renamed to allow single/double precision versions. (ACH) */
  /*   010418  Reduced size of Common block /DLS001/. (ACH) */
  /*   031105  Restored 'own' variables to Common block /DLS001/, to */
  /*           enable interrupt/restart feature. (ACH) */
  /*   050427  Corrected roundoff decrement in TP. (ACH) */
  /* ***END PROLOGUE  DINTDY */
  /* **End */

  /* ***FIRST EXECUTABLE STATEMENT  DINTDY */
  /* Parameter adjustments */
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --dky;

  /* Function Body */
  *iflag = 0;
  if (*k < 0 ||  *k > dls001_1.nq)
  {
    goto L80;
  }
  d__1 = abs(dls001_1.tn) + abs(dls001_1.hu);
  tp = dls001_1.tn - dls001_1.hu - dls001_1.uround * 100. * d_sign(&d__1,
    &dls001_1.hu);
  if ((*t - tp)*(*t - dls001_1.tn) > 0.)
  {
    goto L90;
  }

  s = (*t - dls001_1.tn) / dls001_1.h__;
  ic = 1;
  if (*k == 0)
  {
    goto L15;
  }
  jj1 = dls001_1.l -  *k;
  i__1 = dls001_1.nq;
  for (jj = jj1; jj <= i__1; ++jj)
  {
    /* L10: */
    ic *= jj;
  }
  L15: c__ = (doublereal)ic;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L20: */
    dky[i__] = c__ * yh[i__ + dls001_1.l * yh_dim1];
  }
  if (*k == dls001_1.nq)
  {
    goto L55;
  }
  jb2 = dls001_1.nq -  *k;
  i__1 = jb2;
  for (jb = 1; jb <= i__1; ++jb)
  {
    j = dls001_1.nq - jb;
    jp1 = j + 1;
    ic = 1;
    if (*k == 0)
    {
      goto L35;
    }
    jj1 = jp1 -  *k;
    i__2 = j;
    for (jj = jj1; jj <= i__2; ++jj)
    {
      /* L30: */
      ic *= jj;
    }
    L35: c__ = (doublereal)ic;
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L40: */
      dky[i__] = c__ * yh[i__ + jp1 * yh_dim1] + s * dky[i__];
    }
    /* L50: */
  }
  if (*k == 0)
  {
    return 0;
  }
  L55: i__1 =  - (*k);
  r__ = pow_di(&dls001_1.h__, &i__1);
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L60: */
    dky[i__] = r__ * dky[i__];
  }
  return 0;

  L80: s_copy(msg, "DINTDY-  K (=I1) illegal      ", (ftnlen)80, (ftnlen)30);
  xerrwd_(msg, &c__30, &c__51, &c__0, &c__1, k, &c__0, &c__0, &c_b34, &c_b34, 
    (ftnlen)80);
  *iflag =  - 1;
  return 0;
  L90: s_copy(msg, "DINTDY-  T (=R1) illegal      ", (ftnlen)80, (ftnlen)30);
  xerrwd_(msg, &c__30, &c__52, &c__0, &c__0, &c__0, &c__0, &c__1, t, &c_b34, 
    (ftnlen)80);
  s_copy(msg, "      T not in interval TCUR - HU (= R1) to TCUR (=R2)      ", 
    (ftnlen)80, (ftnlen)60);
  xerrwd_(msg, &c__60, &c__52, &c__0, &c__0, &c__0, &c__0, &c__2, &tp,
    &dls001_1.tn, (ftnlen)80);
  *iflag =  - 2;
  return 0;
  /* ----------------------- END OF SUBROUTINE DINTDY ---------------------- */
} /* dintdy_ */

/* DECK DPREPJ */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dprepj_
   (integer *neq, doublereal *y, doublereal *yh, integer *nyh, doublereal *ewt,
   doublereal *ftem, doublereal *savf, doublereal *wm, integer *iwm, S_fp f,
   S_fp jac)
{
  /* System generated locals */
  integer yh_dim1, yh_offset, i__1, i__2, i__3, i__4;
  doublereal d__1, d__2;

  /* Local variables */
  static integer i__, j;
  static doublereal r__;
  static integer i1, i2, j1;
  static doublereal r0, di;
  static integer ii, jj, ml, mu;
  static doublereal yi, yj, hl0;
  static integer ml3, np1;
  static doublereal fac;
  static integer mba, ier;
  static doublereal con, yjj;
  static integer meb1, lenp;
  static doublereal srur;
  static integer mband, meband;

  /* ***BEGIN PROLOGUE  DPREPJ */
  /* ***SUBSIDIARY */
  /* ***PURPOSE  Compute and process Newton iteration matrix. */
  /* ***TYPE      DOUBLE PRECISION (SPREPJ-S, DPREPJ-D) */
  /* ***AUTHOR  Hindmarsh, Alan C., (LLNL) */
  /* ***DESCRIPTION */

  /*  DPREPJ is called by DSTODE to compute and process the matrix */
  /*  P = I - h*el(1)*J , where J is an approximation to the Jacobian. */
  /*  Here J is computed by the user-supplied routine JAC if */
  /*  MITER = 1 or 4, or by finite differencing if MITER = 2, 3, or 5. */
  /*  If MITER = 3, a diagonal approximation to J is used. */
  /*  J is stored in WM and replaced by P.  If MITER .ne. 3, P is then */
  /*  subjected to LU decomposition in preparation for later solution */
  /*  of linear systems with P as coefficient matrix.  This is done */
  /*  by DGEFA if MITER = 1 or 2, and by DGBFA if MITER = 4 or 5. */

  /*  In addition to variables described in DSTODE and DLSODE prologues, */
  /*  communication with DPREPJ uses the following: */
  /*  Y     = array containing predicted values on entry. */
  /*  FTEM  = work array of length N (ACOR in DSTODE). */
  /*  SAVF  = array containing f evaluated at predicted y. */
  /*  WM    = real work space for matrices.  On output it contains the */
  /*          inverse diagonal matrix if MITER = 3 and the LU decomposition */
  /*          of P if MITER is 1, 2 , 4, or 5. */
  /*          Storage of matrix elements starts at WM(3). */
  /*          WM also contains the following matrix-related data: */
  /*          WM(1) = SQRT(UROUND), used in numerical Jacobian increments. */
  /*          WM(2) = H*EL0, saved for later use if MITER = 3. */
  /*  IWM   = integer work space containing pivot information, starting at */
  /*          IWM(21), if MITER is 1, 2, 4, or 5.  IWM also contains band */
  /*          parameters ML = IWM(1) and MU = IWM(2) if MITER is 4 or 5. */
  /*  EL0   = EL(1) (input). */
  /*  IERPJ = output error flag,  = 0 if no trouble, .gt. 0 if */
  /*          P matrix found to be singular. */
  /*  JCUR  = output flag = 1 to indicate that the Jacobian matrix */
  /*          (or approximation) is now current. */
  /*  This routine also uses the COMMON variables EL0, H, TN, UROUND, */
  /*  MITER, N, NFE, and NJE. */

  /* ***SEE ALSO  DLSODE */
  /* ***ROUTINES CALLED  DGBFA, DGEFA, DVNORM */
  /* ***COMMON BLOCKS    DLS001 */
  /* ***REVISION HISTORY  (YYMMDD) */
  /*   791129  DATE WRITTEN */
  /*   890501  Modified prologue to SLATEC/LDOC format.  (FNF) */
  /*   890504  Minor cosmetic changes.  (FNF) */
  /*   930809  Renamed to allow single/double precision versions. (ACH) */
  /*   010418  Reduced size of Common block /DLS001/. (ACH) */
  /*   031105  Restored 'own' variables to Common block /DLS001/, to */
  /*           enable interrupt/restart feature. (ACH) */
  /* ***END PROLOGUE  DPREPJ */
  /* **End */

  /* ***FIRST EXECUTABLE STATEMENT  DPREPJ */
  /* Parameter adjustments */
  --neq;
  --y;
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --ewt;
  --ftem;
  --savf;
  --wm;
  --iwm;

  /* Function Body */
  ++dls001_1.nje;
  dls001_1.ierpj = 0;
  dls001_1.jcur = 1;
  hl0 = dls001_1.h__ * dls001_1.el0;
  switch (dls001_1.miter)
  {
    case 1:
      goto L100;
    case 2:
      goto L200;
    case 3:
      goto L300;
    case 4:
      goto L400;
    case 5:
      goto L500;
  }
  /* If MITER = 1, call JAC and multiply by scalar. ----------------------- */
  L100: lenp = dls001_1.n * dls001_1.n;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L110: */
    wm[i__ + 2] = 0.;
  }
  (*jac)(&neq[1], &dls001_1.tn, &y[1], &c__0, &c__0, &wm[3], &dls001_1.n);
  con =  - hl0;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L120: */
    wm[i__ + 2] *= con;
  }
  goto L240;
  /* If MITER = 2, make N calls to F to approximate J. -------------------- */
  L200: fac = dvnorm_(&dls001_1.n, &savf[1], &ewt[1]);
  r0 = abs(dls001_1.h__) *1e3 * dls001_1.uround * dls001_1.n * fac;
  if (r0 == 0.)
  {
    r0 = 1.;
  }
  srur = wm[1];
  j1 = 2;
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    yj = y[j];
    /* Computing MAX */
    d__1 = srur * abs(yj), d__2 = r0 / ewt[j];
    r__ = max(d__1, d__2);
    y[j] += r__;
    fac =  - hl0 / r__;
    (*f)(&neq[1], &dls001_1.tn, &y[1], &ftem[1]);
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L220: */
      wm[i__ + j1] = (ftem[i__] - savf[i__]) *fac;
    }
    y[j] = yj;
    j1 += dls001_1.n;
    /* L230: */
  }
  dls001_1.nfe += dls001_1.n;
  /* Add identity matrix. ------------------------------------------------- */
  L240: j = 3;
  np1 = dls001_1.n + 1;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    wm[j] += 1.;
    /* L250: */
    j += np1;
  }
  /* Do LU decomposition on P. -------------------------------------------- */
  dgefa_(&wm[3], &dls001_1.n, &dls001_1.n, &iwm[21], &ier);
  if (ier != 0)
  {
    dls001_1.ierpj = 1;
  }
  return 0;
  /* If MITER = 3, construct a diagonal approximation to J and P. --------- */
  L300: wm[2] = hl0;
  r__ = dls001_1.el0 * .1;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L310: */
    y[i__] += r__ *(dls001_1.h__ *savf[i__] - yh[i__ + (yh_dim1 << 1)]);
  }
  (*f)(&neq[1], &dls001_1.tn, &y[1], &wm[3]);
  ++dls001_1.nfe;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    r0 = dls001_1.h__ * savf[i__] - yh[i__ + (yh_dim1 << 1)];
    di = r0 * .1 - dls001_1.h__ *(wm[i__ + 2] - savf[i__]);
    wm[i__ + 2] = 1.;
    if (abs(r0) < dls001_1.uround / ewt[i__])
    {
      goto L320;
    }
    if (abs(di) == 0.)
    {
      goto L330;
    }
    wm[i__ + 2] = r0 * .1 / di;
    L320: ;
  }
  return 0;
  L330: dls001_1.ierpj = 1;
  return 0;
  /* If MITER = 4, call JAC and multiply by scalar. ----------------------- */
  L400: ml = iwm[1];
  mu = iwm[2];
  ml3 = ml + 3;
  mband = ml + mu + 1;
  meband = mband + ml;
  lenp = meband * dls001_1.n;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L410: */
    wm[i__ + 2] = 0.;
  }
  (*jac)(&neq[1], &dls001_1.tn, &y[1], &ml, &mu, &wm[ml3], &meband);
  con =  - hl0;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L420: */
    wm[i__ + 2] *= con;
  }
  goto L570;
  /* If MITER = 5, make MBAND calls to F to approximate J. ---------------- */
  L500: ml = iwm[1];
  mu = iwm[2];
  mband = ml + mu + 1;
  mba = min(mband, dls001_1.n);
  meband = mband + ml;
  meb1 = meband - 1;
  srur = wm[1];
  fac = dvnorm_(&dls001_1.n, &savf[1], &ewt[1]);
  r0 = abs(dls001_1.h__) *1e3 * dls001_1.uround * dls001_1.n * fac;
  if (r0 == 0.)
  {
    r0 = 1.;
  }
  i__1 = mba;
  for (j = 1; j <= i__1; ++j)
  {
    i__2 = dls001_1.n;
    i__3 = mband;
    for (i__ = j; i__3 < 0 ? i__ >= i__2: i__ <= i__2; i__ += i__3)
    {
      yi = y[i__];
      /* Computing MAX */
      d__1 = srur * abs(yi), d__2 = r0 / ewt[i__];
      r__ = max(d__1, d__2);
      /* L530: */
      y[i__] += r__;
    }
    (*f)(&neq[1], &dls001_1.tn, &y[1], &ftem[1]);
    i__3 = dls001_1.n;
    i__2 = mband;
    for (jj = j; i__2 < 0 ? jj >= i__3: jj <= i__3; jj += i__2)
    {
      y[jj] = yh[jj + yh_dim1];
      yjj = y[jj];
      /* Computing MAX */
      d__1 = srur * abs(yjj), d__2 = r0 / ewt[jj];
      r__ = max(d__1, d__2);
      fac =  - hl0 / r__;
      /* Computing MAX */
      i__4 = jj - mu;
      i1 = max(i__4, 1);
      /* Computing MIN */
      i__4 = jj + ml;
      i2 = min(i__4, dls001_1.n);
      ii = jj * meb1 - ml + 2;
      i__4 = i2;
      for (i__ = i1; i__ <= i__4; ++i__)
      {
        /* L540: */
        wm[ii + i__] = (ftem[i__] - savf[i__]) *fac;
      }
      /* L550: */
    }
    /* L560: */
  }
  dls001_1.nfe += mba;
  /* Add identity matrix. ------------------------------------------------- */
  L570: ii = mband + 2;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    wm[ii] += 1.;
    /* L580: */
    ii += meband;
  }
  /* Do LU decomposition of P. -------------------------------------------- */
  dgbfa_(&wm[3], &meband, &dls001_1.n, &ml, &mu, &iwm[21], &ier);
  if (ier != 0)
  {
    dls001_1.ierpj = 1;
  }
  return 0;
  /* ----------------------- END OF SUBROUTINE DPREPJ ---------------------- */
} /* dprepj_ */

/* DECK DSOLSY */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dsolsy_
   (doublereal *wm, integer *iwm, doublereal *x, doublereal *tem)
{
  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__;
  static doublereal r__, di;
  static integer ml, mu;
  static doublereal hl0, phl0;
  static integer meband;

  /* ***BEGIN PROLOGUE  DSOLSY */
  /* ***SUBSIDIARY */
  /* ***PURPOSE  ODEPACK linear system solver. */
  /* ***TYPE      DOUBLE PRECISION (SSOLSY-S, DSOLSY-D) */
  /* ***AUTHOR  Hindmarsh, Alan C., (LLNL) */
  /* ***DESCRIPTION */

  /*  This routine manages the solution of the linear system arising from */
  /*  a chord iteration.  It is called if MITER .ne. 0. */
  /*  If MITER is 1 or 2, it calls DGESL to accomplish this. */
  /*  If MITER = 3 it updates the coefficient h*EL0 in the diagonal */
  /*  matrix, and then computes the solution. */
  /*  If MITER is 4 or 5, it calls DGBSL. */
  /*  Communication with DSOLSY uses the following variables: */
  /*  WM    = real work space containing the inverse diagonal matrix if */
  /*          MITER = 3 and the LU decomposition of the matrix otherwise. */
  /*          Storage of matrix elements starts at WM(3). */
  /*          WM also contains the following matrix-related data: */
  /*          WM(1) = SQRT(UROUND) (not used here), */
  /*          WM(2) = HL0, the previous value of h*EL0, used if MITER = 3. */
  /*  IWM   = integer work space containing pivot information, starting at */
  /*          IWM(21), if MITER is 1, 2, 4, or 5.  IWM also contains band */
  /*          parameters ML = IWM(1) and MU = IWM(2) if MITER is 4 or 5. */
  /*  X     = the right-hand side vector on input, and the solution vector */
  /*          on output, of length N. */
  /*  TEM   = vector of work space of length N, not used in this version. */
  /*  IERSL = output flag (in COMMON).  IERSL = 0 if no trouble occurred. */
  /*          IERSL = 1 if a singular matrix arose with MITER = 3. */
  /*  This routine also uses the COMMON variables EL0, H, MITER, and N. */

  /* ***SEE ALSO  DLSODE */
  /* ***ROUTINES CALLED  DGBSL, DGESL */
  /* ***COMMON BLOCKS    DLS001 */
  /* ***REVISION HISTORY  (YYMMDD) */
  /*   791129  DATE WRITTEN */
  /*   890501  Modified prologue to SLATEC/LDOC format.  (FNF) */
  /*   890503  Minor cosmetic changes.  (FNF) */
  /*   930809  Renamed to allow single/double precision versions. (ACH) */
  /*   010418  Reduced size of Common block /DLS001/. (ACH) */
  /*   031105  Restored 'own' variables to Common block /DLS001/, to */
  /*           enable interrupt/restart feature. (ACH) */
  /* ***END PROLOGUE  DSOLSY */
  /* **End */

  /* ***FIRST EXECUTABLE STATEMENT  DSOLSY */
  /* Parameter adjustments */
  --tem;
  --x;
  --iwm;
  --wm;

  /* Function Body */
  dls001_1.iersl = 0;
  switch (dls001_1.miter)
  {
    case 1:
      goto L100;
    case 2:
      goto L100;
    case 3:
      goto L300;
    case 4:
      goto L400;
    case 5:
      goto L400;
  }
  L100: dgesl_(&wm[3], &dls001_1.n, &dls001_1.n, &iwm[21], &x[1], &c__0);
  return 0;

  L300: phl0 = wm[2];
  hl0 = dls001_1.h__ * dls001_1.el0;
  wm[2] = hl0;
  if (hl0 == phl0)
  {
    goto L330;
  }
  r__ = hl0 / phl0;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    di = 1. - r__ *(1. - 1. / wm[i__ + 2]);
    if (abs(di) == 0.)
    {
      goto L390;
    }
    /* L320: */
    wm[i__ + 2] = 1. / di;
  }
  L330: i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L340: */
    x[i__] = wm[i__ + 2] *x[i__];
  }
  return 0;
  L390: dls001_1.iersl = 1;
  return 0;

  L400: ml = iwm[1];
  mu = iwm[2];
  meband = (ml << 1) + mu + 1;
  dgbsl_(&wm[3], &meband, &dls001_1.n, &ml, &mu, &iwm[21], &x[1], &c__0);
  return 0;
  /* ----------------------- END OF SUBROUTINE DSOLSY ---------------------- */
} /* dsolsy_ */

/* DECK DSRCOM */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dsrcom_
   (doublereal *rsav, integer *isav, integer *job)
{
  /* Initialized data */

  static integer lenrls = 218;
  static integer lenils = 37;

  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__;

  /* ***BEGIN PROLOGUE  DSRCOM */
  /* ***SUBSIDIARY */
  /* ***PURPOSE  Save/restore ODEPACK COMMON blocks. */
  /* ***TYPE      DOUBLE PRECISION (SSRCOM-S, DSRCOM-D) */
  /* ***AUTHOR  Hindmarsh, Alan C., (LLNL) */
  /* ***DESCRIPTION */

  /*  This routine saves or restores (depending on JOB) the contents of */
  /*  the COMMON block DLS001, which is used internally */
  /*  by one or more ODEPACK solvers. */

  /*  RSAV = real array of length 218 or more. */
  /*  ISAV = integer array of length 37 or more. */
  /*  JOB  = flag indicating to save or restore the COMMON blocks: */
  /*         JOB  = 1 if COMMON is to be saved (written to RSAV/ISAV) */
  /*         JOB  = 2 if COMMON is to be restored (read from RSAV/ISAV) */
  /*         A call with JOB = 2 presumes a prior call with JOB = 1. */

  /* ***SEE ALSO  DLSODE */
  /* ***ROUTINES CALLED  (NONE) */
  /* ***COMMON BLOCKS    DLS001 */
  /* ***REVISION HISTORY  (YYMMDD) */
  /*   791129  DATE WRITTEN */
  /*   890501  Modified prologue to SLATEC/LDOC format.  (FNF) */
  /*   890503  Minor cosmetic changes.  (FNF) */
  /*   921116  Deleted treatment of block /EH0001/.  (ACH) */
  /*   930801  Reduced Common block length by 2.  (ACH) */
  /*   930809  Renamed to allow single/double precision versions. (ACH) */
  /*   010418  Reduced Common block length by 209+12. (ACH) */
  /*   031105  Restored 'own' variables to Common block /DLS001/, to */
  /*           enable interrupt/restart feature. (ACH) */
  /*   031112  Added SAVE statement for data-loaded constants. */
  /* ***END PROLOGUE  DSRCOM */
  /* **End */
  /* Parameter adjustments */
  --isav;
  --rsav;

  /* Function Body */

  /* ***FIRST EXECUTABLE STATEMENT  DSRCOM */
  if (*job == 2)
  {
    goto L100;
  }

  i__1 = lenrls;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    rsav[i__] = dls001_2.rls[i__ - 1];
  }
  i__1 = lenils;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L20: */
    isav[i__] = dls001_2.ils[i__ - 1];
  }
  return 0;

  L100: i__1 = lenrls;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L110: */
    dls001_2.rls[i__ - 1] = rsav[i__];
  }
  i__1 = lenils;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L120: */
    dls001_2.ils[i__ - 1] = isav[i__];
  }
  return 0;
  /* ----------------------- END OF SUBROUTINE DSRCOM ---------------------- */
} /* dsrcom_ */

/* DECK DSTODE */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dstode_
   (integer *neq, doublereal *y, doublereal *yh, integer *nyh, doublereal *yh1,
   doublereal *ewt, doublereal *savf, doublereal *acor, doublereal *wm, integer
   *iwm, S_fp f, U_fp jac, S_fp pjac, S_fp slvs)
{
  /* System generated locals */
  integer yh_dim1, yh_offset, i__1, i__2;
  doublereal d__1, d__2, d__3;

  /* Local variables */
  static integer i__, j, m;
  static doublereal r__;
  static integer i1, jb;
  static doublereal rh, del, ddn;
  static integer ncf;
  static doublereal dsm, dup, dcon, delp, rhdn, exdn;
  static integer iret;
  static doublereal told, rhsm;
  static integer newq;
  static doublereal exsm, rhup, exup;
  static integer iredo;

  /* ***BEGIN PROLOGUE  DSTODE */
  /* ***SUBSIDIARY */
  /* ***PURPOSE  Performs one step of an ODEPACK integration. */
  /* ***TYPE      DOUBLE PRECISION (SSTODE-S, DSTODE-D) */
  /* ***AUTHOR  Hindmarsh, Alan C., (LLNL) */
  /* ***DESCRIPTION */

  /*  DSTODE performs one step of the integration of an initial value */
  /*  problem for a system of ordinary differential equations. */
  /*  Note:  DSTODE is independent of the value of the iteration method */
  /*  indicator MITER, when this is .ne. 0, and hence is independent */
  /*  of the type of chord method used, or the Jacobian structure. */
  /*  Communication with DSTODE is done with the following variables: */

  /*  NEQ    = integer array containing problem size in NEQ(1), and */
  /*           passed as the NEQ argument in all calls to F and JAC. */
  /*  Y      = an array of length .ge. N used as the Y argument in */
  /*           all calls to F and JAC. */
  /*  YH     = an NYH by LMAX array containing the dependent variables */
  /*           and their approximate scaled derivatives, where */
  /*           LMAX = MAXORD + 1.  YH(i,j+1) contains the approximate */
  /*           j-th derivative of y(i), scaled by h**j/factorial(j) */
  /*           (j = 0,1,...,NQ).  on entry for the first step, the first */
  /*           two columns of YH must be set from the initial values. */
  /*  NYH    = a constant integer .ge. N, the first dimension of YH. */
  /*  YH1    = a one-dimensional array occupying the same space as YH. */
  /*  EWT    = an array of length N containing multiplicative weights */
  /*           for local error measurements.  Local errors in Y(i) are */
  /*           compared to 1.0/EWT(i) in various error tests. */
  /*  SAVF   = an array of working storage, of length N. */
  /*           Also used for input of YH(*,MAXORD+2) when JSTART = -1 */
  /*           and MAXORD .lt. the current order NQ. */
  /*  ACOR   = a work array of length N, used for the accumulated */
  /*           corrections.  On a successful return, ACOR(i) contains */
  /*           the estimated one-step local error in Y(i). */
  /*  WM,IWM = real and integer work arrays associated with matrix */
  /*           operations in chord iteration (MITER .ne. 0). */
  /*  PJAC   = name of routine to evaluate and preprocess Jacobian matrix */
  /*           and P = I - h*el0*JAC, if a chord method is being used. */
  /*  SLVS   = name of routine to solve linear system in chord iteration. */
  /*  CCMAX  = maximum relative change in h*el0 before PJAC is called. */
  /*  H      = the step size to be attempted on the next step. */
  /*           H is altered by the error control algorithm during the */
  /*           problem.  H can be either positive or negative, but its */
  /*           sign must remain constant throughout the problem. */
  /*  HMIN   = the minimum absolute value of the step size h to be used. */
  /*  HMXI   = inverse of the maximum absolute value of h to be used. */
  /*           HMXI = 0.0 is allowed and corresponds to an infinite hmax. */
  /*           HMIN and HMXI may be changed at any time, but will not */
  /*           take effect until the next change of h is considered. */
  /*  TN     = the independent variable. TN is updated on each step taken. */
  /*  JSTART = an integer used for input only, with the following */
  /*           values and meanings: */
  /*                0  perform the first step. */
  /*            .gt.0  take a new step continuing from the last. */
  /*               -1  take the next step with a new value of H, MAXORD, */
  /*                     N, METH, MITER, and/or matrix parameters. */
  /*               -2  take the next step with a new value of H, */
  /*                     but with other inputs unchanged. */
  /*           On return, JSTART is set to 1 to facilitate continuation. */
  /*  KFLAG  = a completion code with the following meanings: */
  /*                0  the step was succesful. */
  /*               -1  the requested error could not be achieved. */
  /*               -2  corrector convergence could not be achieved. */
  /*               -3  fatal error in PJAC or SLVS. */
  /*           A return with KFLAG = -1 or -2 means either */
  /*           abs(H) = HMIN or 10 consecutive failures occurred. */
  /*           On a return with KFLAG negative, the values of TN and */
  /*           the YH array are as of the beginning of the last */
  /*           step, and H is the last step size attempted. */
  /*  MAXORD = the maximum order of integration method to be allowed. */
  /*  MAXCOR = the maximum number of corrector iterations allowed. */
  /*  MSBP   = maximum number of steps between PJAC calls (MITER .gt. 0). */
  /*  MXNCF  = maximum number of convergence failures allowed. */
  /*  METH/MITER = the method flags.  See description in driver. */
  /*  N      = the number of first-order differential equations. */
  /*  The values of CCMAX, H, HMIN, HMXI, TN, JSTART, KFLAG, MAXORD, */
  /*  MAXCOR, MSBP, MXNCF, METH, MITER, and N are communicated via COMMON. */

  /* ***SEE ALSO  DLSODE */
  /* ***ROUTINES CALLED  DCFODE, DVNORM */
  /* ***COMMON BLOCKS    DLS001 */
  /* ***REVISION HISTORY  (YYMMDD) */
  /*   791129  DATE WRITTEN */
  /*   890501  Modified prologue to SLATEC/LDOC format.  (FNF) */
  /*   890503  Minor cosmetic changes.  (FNF) */
  /*   930809  Renamed to allow single/double precision versions. (ACH) */
  /*   010418  Reduced size of Common block /DLS001/. (ACH) */
  /*   031105  Restored 'own' variables to Common block /DLS001/, to */
  /*           enable interrupt/restart feature. (ACH) */
  /* ***END PROLOGUE  DSTODE */
  /* **End */

  /* ***FIRST EXECUTABLE STATEMENT  DSTODE */
  /* Parameter adjustments */
  --neq;
  --y;
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --yh1;
  --ewt;
  --savf;
  --acor;
  --wm;
  --iwm;

  /* Function Body */
  dls001_3.kflag = 0;
  told = dls001_3.tn;
  ncf = 0;
  dls001_3.ierpj = 0;
  dls001_3.iersl = 0;
  dls001_3.jcur = 0;
  dls001_3.icf = 0;
  delp = 0.;
  if (dls001_3.jstart > 0)
  {
    goto L200;
  }
  if (dls001_3.jstart ==  - 1)
  {
    goto L100;
  }
  if (dls001_3.jstart ==  - 2)
  {
    goto L160;
  }
  /* ----------------------------------------------------------------------- */
  /* On the first call, the order is set to 1, and other variables are */
  /* initialized.  RMAX is the maximum ratio by which H can be increased */
  /* in a single step.  It is initially 1.E4 to compensate for the small */
  /* initial H, but then is normally equal to 10.  If a failure */
  /* occurs (in corrector convergence or error test), RMAX is set to 2 */
  /* for the next increase. */
  /* ----------------------------------------------------------------------- */
  dls001_3.lmax = dls001_3.maxord + 1;
  dls001_3.nq = 1;
  dls001_3.l = 2;
  dls001_3.ialth = 2;
  dls001_3.rmax = 1e4;
  dls001_3.rc = 0.;
  dls001_3.el0 = 1.;
  dls001_3.crate = .7;
  dls001_3.hold = dls001_3.h__;
  dls001_3.meo = dls001_3.meth;
  dls001_3.nslp = 0;
  dls001_3.ipup = dls001_3.miter;
  iret = 3;
  goto L140;
  /* ----------------------------------------------------------------------- */
  /* The following block handles preliminaries needed when JSTART = -1. */
  /* IPUP is set to MITER to force a matrix update. */
  /* If an order increase is about to be considered (IALTH = 1), */
  /* IALTH is reset to 2 to postpone consideration one more step. */
  /* If the caller has changed METH, DCFODE is called to reset */
  /* the coefficients of the method. */
  /* If the caller has changed MAXORD to a value less than the current */
  /* order NQ, NQ is reduced to MAXORD, and a new H chosen accordingly. */
  /* If H is to be changed, YH must be rescaled. */
  /* If H or METH is being changed, IALTH is reset to L = NQ + 1 */
  /* to prevent further changes in H for that many steps. */
  /* ----------------------------------------------------------------------- */
  L100: dls001_3.ipup = dls001_3.miter;
  dls001_3.lmax = dls001_3.maxord + 1;
  if (dls001_3.ialth == 1)
  {
    dls001_3.ialth = 2;
  }
  if (dls001_3.meth == dls001_3.meo)
  {
    goto L110;
  }
  dcfode_(&dls001_3.meth, dls001_3.elco, dls001_3.tesco);
  dls001_3.meo = dls001_3.meth;
  if (dls001_3.nq > dls001_3.maxord)
  {
    goto L120;
  }
  dls001_3.ialth = dls001_3.l;
  iret = 1;
  goto L150;
  L110: if (dls001_3.nq <= dls001_3.maxord)
  {
    goto L160;
  }
  L120: dls001_3.nq = dls001_3.maxord;
  dls001_3.l = dls001_3.lmax;
  i__1 = dls001_3.l;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L125: */
    dls001_3.el[i__ - 1] = dls001_3.elco[i__ + dls001_3.nq * 13-14];
  }
  dls001_3.nqnyh = dls001_3.nq **nyh;
  dls001_3.rc = dls001_3.rc * dls001_3.el[0] / dls001_3.el0;
  dls001_3.el0 = dls001_3.el[0];
  dls001_3.conit = .5 / (dls001_3.nq + 2);
  ddn = dvnorm_(&dls001_3.n, &savf[1], &ewt[1]) / dls001_3.tesco[dls001_3.l*3
    -3];
  exdn = 1. / dls001_3.l;
  rhdn = 1. / (pow_dd(&ddn, &exdn) *1.3 + 1.3e-6);
  rh = min(rhdn, 1.);
  iredo = 3;
  if (dls001_3.h__ == dls001_3.hold)
  {
    goto L170;
  }
  /* Computing MIN */
  d__2 = rh, d__3 = (d__1 = dls001_3.h__ / dls001_3.hold, abs(d__1));
  rh = min(d__2, d__3);
  dls001_3.h__ = dls001_3.hold;
  goto L175;
  /* ----------------------------------------------------------------------- */
  /* DCFODE is called to get all the integration coefficients for the */
  /* current METH.  Then the EL vector and related constants are reset */
  /* whenever the order NQ is changed, or at the start of the problem. */
  /* ----------------------------------------------------------------------- */
  L140: dcfode_(&dls001_3.meth, dls001_3.elco, dls001_3.tesco);
  L150: i__1 = dls001_3.l;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L155: */
    dls001_3.el[i__ - 1] = dls001_3.elco[i__ + dls001_3.nq * 13-14];
  }
  dls001_3.nqnyh = dls001_3.nq **nyh;
  dls001_3.rc = dls001_3.rc * dls001_3.el[0] / dls001_3.el0;
  dls001_3.el0 = dls001_3.el[0];
  dls001_3.conit = .5 / (dls001_3.nq + 2);
  switch (iret)
  {
    case 1:
      goto L160;
    case 2:
      goto L170;
    case 3:
      goto L200;
  }
  /* ----------------------------------------------------------------------- */
  /* If H is being changed, the H ratio RH is checked against */
  /* RMAX, HMIN, and HMXI, and the YH array rescaled.  IALTH is set to */
  /* L = NQ + 1 to prevent a change of H for that many steps, unless */
  /* forced by a convergence or error test failure. */
  /* ----------------------------------------------------------------------- */
  L160: if (dls001_3.h__ == dls001_3.hold)
  {
    goto L200;
  }
  rh = dls001_3.h__ / dls001_3.hold;
  dls001_3.h__ = dls001_3.hold;
  iredo = 3;
  goto L175;
  L170: 
  /* Computing MAX */
  d__1 = rh, d__2 = dls001_3.hmin / abs(dls001_3.h__);
  rh = max(d__1, d__2);
  L175: rh = min(rh, dls001_3.rmax);
  /* Computing MAX */
  d__1 = 1., d__2 = abs(dls001_3.h__) *dls001_3.hmxi * rh;
  rh /= max(d__1, d__2);
  r__ = 1.;
  i__1 = dls001_3.l;
  for (j = 2; j <= i__1; ++j)
  {
    r__ *= rh;
    i__2 = dls001_3.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L180: */
      yh[i__ + j * yh_dim1] *= r__;
    }
  }
  dls001_3.h__ *= rh;
  dls001_3.rc *= rh;
  dls001_3.ialth = dls001_3.l;
  if (iredo == 0)
  {
    goto L690;
  }
  /* ----------------------------------------------------------------------- */
  /* This section computes the predicted values by effectively */
  /* multiplying the YH array by the Pascal Triangle matrix. */
  /* RC is the ratio of new to old values of the coefficient  H*EL(1). */
  /* When RC differs from 1 by more than CCMAX, IPUP is set to MITER */
  /* to force PJAC to be called, if a Jacobian is involved. */
  /* In any case, PJAC is called at least every MSBP steps. */
  /* ----------------------------------------------------------------------- */
  L200: if ((d__1 = dls001_3.rc - 1., abs(d__1)) > dls001_3.ccmax)
  {
    dls001_3.ipup = dls001_3.miter;
  }
  if (dls001_3.nst >= dls001_3.nslp + dls001_3.msbp)
  {
    dls001_3.ipup = dls001_3.miter;
  }
  dls001_3.tn += dls001_3.h__;
  i1 = dls001_3.nqnyh + 1;
  i__2 = dls001_3.nq;
  for (jb = 1; jb <= i__2; ++jb)
  {
    i1 -=  *nyh;
    /* dir$ ivdep */
    i__1 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__1; ++i__)
    {
      /* L210: */
      yh1[i__] += yh1[i__ +  *nyh];
    }
    /* L215: */
  }
  /* ----------------------------------------------------------------------- */
  /* Up to MAXCOR corrector iterations are taken.  A convergence test is */
  /* made on the R.M.S. norm of each correction, weighted by the error */
  /* weight vector EWT.  The sum of the corrections is accumulated in the */
  /* vector ACOR(i).  The YH array is not altered in the corrector loop. */
  /* ----------------------------------------------------------------------- */
  L220: m = 0;
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L230: */
    y[i__] = yh[i__ + yh_dim1];
  }
  (*f)(&neq[1], &dls001_3.tn, &y[1], &savf[1]);
  ++dls001_3.nfe;
  if (dls001_3.ipup <= 0)
  {
    goto L250;
  }
  /* ----------------------------------------------------------------------- */
  /* If indicated, the matrix P = I - h*el(1)*J is reevaluated and */
  /* preprocessed before starting the corrector iteration.  IPUP is set */
  /* to 0 as an indicator that this has been done. */
  /* ----------------------------------------------------------------------- */
  (*pjac)(&neq[1], &y[1], &yh[yh_offset], nyh, &ewt[1], &acor[1], &savf[1], 
    &wm[1], &iwm[1], (S_fp)f, (U_fp)jac);
  dls001_3.ipup = 0;
  dls001_3.rc = 1.;
  dls001_3.nslp = dls001_3.nst;
  dls001_3.crate = .7;
  if (dls001_3.ierpj != 0)
  {
    goto L430;
  }
  L250: i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L260: */
    acor[i__] = 0.;
  }
  L270: if (dls001_3.miter != 0)
  {
    goto L350;
  }
  /* ----------------------------------------------------------------------- */
  /* In the case of functional iteration, update Y directly from */
  /* the result of the last function evaluation. */
  /* ----------------------------------------------------------------------- */
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    savf[i__] = dls001_3.h__ * savf[i__] - yh[i__ + (yh_dim1 << 1)];
    /* L290: */
    y[i__] = savf[i__] - acor[i__];
  }
  del = dvnorm_(&dls001_3.n, &y[1], &ewt[1]);
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    y[i__] = yh[i__ + yh_dim1] + dls001_3.el[0] *savf[i__];
    /* L300: */
    acor[i__] = savf[i__];
  }
  goto L400;
  /* ----------------------------------------------------------------------- */
  /* In the case of the chord method, compute the corrector error, */
  /* and solve the linear system with that as right-hand side and */
  /* P as coefficient matrix. */
  /* ----------------------------------------------------------------------- */
  L350: i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L360: */
    y[i__] = dls001_3.h__ * savf[i__] - (yh[i__ + (yh_dim1 << 1)] + acor[i__]);
  }
  (*slvs)(&wm[1], &iwm[1], &y[1], &savf[1]);
  if (dls001_3.iersl < 0)
  {
    goto L430;
  }
  if (dls001_3.iersl > 0)
  {
    goto L410;
  }
  del = dvnorm_(&dls001_3.n, &y[1], &ewt[1]);
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    acor[i__] += y[i__];
    /* L380: */
    y[i__] = yh[i__ + yh_dim1] + dls001_3.el[0] *acor[i__];
  }
  /* ----------------------------------------------------------------------- */
  /* Test for convergence.  If M.gt.0, an estimate of the convergence */
  /* rate constant is stored in CRATE, and this is used in the test. */
  /* ----------------------------------------------------------------------- */
  L400: if (m != 0)
  {
    /* Computing MAX */
    d__1 = dls001_3.crate * .2, d__2 = del / delp;
    dls001_3.crate = max(d__1, d__2);
  }
  /* Computing MIN */
  d__1 = 1., d__2 = dls001_3.crate * 1.5;
  dcon = del * min(d__1, d__2) / (dls001_3.tesco[dls001_3.nq *3-2]
    *dls001_3.conit);
  if (dcon <= 1.)
  {
    goto L450;
  }
  ++m;
  if (m == dls001_3.maxcor)
  {
    goto L410;
  }
  if (m >= 2 && del > delp *2.)
  {
    goto L410;
  }
  delp = del;
  (*f)(&neq[1], &dls001_3.tn, &y[1], &savf[1]);
  ++dls001_3.nfe;
  goto L270;
  /* ----------------------------------------------------------------------- */
  /* The corrector iteration failed to converge. */
  /* If MITER .ne. 0 and the Jacobian is out of date, PJAC is called for */
  /* the next try.  Otherwise the YH array is retracted to its values */
  /* before prediction, and H is reduced, if possible.  If H cannot be */
  /* reduced or MXNCF failures have occurred, exit with KFLAG = -2. */
  /* ----------------------------------------------------------------------- */
  L410: if (dls001_3.miter == 0 || dls001_3.jcur == 1)
  {
    goto L430;
  }
  dls001_3.icf = 1;
  dls001_3.ipup = dls001_3.miter;
  goto L220;
  L430: dls001_3.icf = 2;
  ++ncf;
  dls001_3.rmax = 2.;
  dls001_3.tn = told;
  i1 = dls001_3.nqnyh + 1;
  i__2 = dls001_3.nq;
  for (jb = 1; jb <= i__2; ++jb)
  {
    i1 -=  *nyh;
    /* dir$ ivdep */
    i__1 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__1; ++i__)
    {
      /* L440: */
      yh1[i__] -= yh1[i__ +  *nyh];
    }
    /* L445: */
  }
  if (dls001_3.ierpj < 0 || dls001_3.iersl < 0)
  {
    goto L680;
  }
  if (abs(dls001_3.h__) <= dls001_3.hmin *1.00001)
  {
    goto L670;
  }
  if (ncf == dls001_3.mxncf)
  {
    goto L670;
  }
  rh = .25;
  dls001_3.ipup = dls001_3.miter;
  iredo = 1;
  goto L170;
  /* ----------------------------------------------------------------------- */
  /* The corrector has converged.  JCUR is set to 0 */
  /* to signal that the Jacobian involved may need updating later. */
  /* The local error test is made and control passes to statement 500 */
  /* if it fails. */
  /* ----------------------------------------------------------------------- */
  L450: dls001_3.jcur = 0;
  if (m == 0)
  {
    dsm = del / dls001_3.tesco[dls001_3.nq *3-2];
  }
  if (m > 0)
  {
    dsm = dvnorm_(&dls001_3.n, &acor[1], &ewt[1]) / dls001_3.tesco[dls001_3.nq
      *3-2];
  }
  if (dsm > 1.)
  {
    goto L500;
  }
  /* ----------------------------------------------------------------------- */
  /* After a successful step, update the YH array. */
  /* Consider changing H if IALTH = 1.  Otherwise decrease IALTH by 1. */
  /* If IALTH is then 1 and NQ .lt. MAXORD, then ACOR is saved for */
  /* use in a possible order increase on the next step. */
  /* If a change in H is considered, an increase or decrease in order */
  /* by one is considered also.  A change in H is made only if it is by a */
  /* factor of at least 1.1.  If not, IALTH is set to 3 to prevent */
  /* testing for that many steps. */
  /* ----------------------------------------------------------------------- */
  dls001_3.kflag = 0;
  iredo = 0;
  ++dls001_3.nst;
  dls001_3.hu = dls001_3.h__;
  dls001_3.nqu = dls001_3.nq;
  i__2 = dls001_3.l;
  for (j = 1; j <= i__2; ++j)
  {
    i__1 = dls001_3.n;
    for (i__ = 1; i__ <= i__1; ++i__)
    {
      /* L470: */
      yh[i__ + j * yh_dim1] += dls001_3.el[j - 1] *acor[i__];
    }
  }
  --dls001_3.ialth;
  if (dls001_3.ialth == 0)
  {
    goto L520;
  }
  if (dls001_3.ialth > 1)
  {
    goto L700;
  }
  if (dls001_3.l == dls001_3.lmax)
  {
    goto L700;
  }
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L490: */
    yh[i__ + dls001_3.lmax * yh_dim1] = acor[i__];
  }
  goto L700;
  /* ----------------------------------------------------------------------- */
  /* The error test failed.  KFLAG keeps track of multiple failures. */
  /* Restore TN and the YH array to their previous values, and prepare */
  /* to try the step again.  Compute the optimum step size for this or */
  /* one lower order.  After 2 or more failures, H is forced to decrease */
  /* by a factor of 0.2 or less. */
  /* ----------------------------------------------------------------------- */
  L500: --dls001_3.kflag;
  dls001_3.tn = told;
  i1 = dls001_3.nqnyh + 1;
  i__1 = dls001_3.nq;
  for (jb = 1; jb <= i__1; ++jb)
  {
    i1 -=  *nyh;
    /* dir$ ivdep */
    i__2 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__2; ++i__)
    {
      /* L510: */
      yh1[i__] -= yh1[i__ +  *nyh];
    }
    /* L515: */
  }
  dls001_3.rmax = 2.;
  if (abs(dls001_3.h__) <= dls001_3.hmin *1.00001)
  {
    goto L660;
  }
  if (dls001_3.kflag <=  - 3)
  {
    goto L640;
  }
  iredo = 2;
  rhup = 0.;
  goto L540;
  /* ----------------------------------------------------------------------- */
  /* Regardless of the success or failure of the step, factors */
  /* RHDN, RHSM, and RHUP are computed, by which H could be multiplied */
  /* at order NQ - 1, order NQ, or order NQ + 1, respectively. */
  /* In the case of failure, RHUP = 0.0 to avoid an order increase. */
  /* The largest of these is determined and the new order chosen */
  /* accordingly.  If the order is to be increased, we compute one */
  /* additional scaled derivative. */
  /* ----------------------------------------------------------------------- */
  L520: rhup = 0.;
  if (dls001_3.l == dls001_3.lmax)
  {
    goto L540;
  }
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L530: */
    savf[i__] = acor[i__] - yh[i__ + dls001_3.lmax * yh_dim1];
  }
  dup = dvnorm_(&dls001_3.n, &savf[1], &ewt[1]) / dls001_3.tesco[dls001_3.nq *3
    -1];
  exup = 1. / (dls001_3.l + 1);
  rhup = 1. / (pow_dd(&dup, &exup) *1.4 + 1.4e-6);
  L540: exsm = 1. / dls001_3.l;
  rhsm = 1. / (pow_dd(&dsm, &exsm) *1.2 + 1.2e-6);
  rhdn = 0.;
  if (dls001_3.nq == 1)
  {
    goto L560;
  }
  ddn = dvnorm_(&dls001_3.n, &yh[dls001_3.l *yh_dim1 + 1], &ewt[1]) /
    dls001_3.tesco[dls001_3.nq *3-3];
  exdn = 1. / dls001_3.nq;
  rhdn = 1. / (pow_dd(&ddn, &exdn) *1.3 + 1.3e-6);
  L560: if (rhsm >= rhup)
  {
    goto L570;
  }
  if (rhup > rhdn)
  {
    goto L590;
  }
  goto L580;
  L570: if (rhsm < rhdn)
  {
    goto L580;
  }
  newq = dls001_3.nq;
  rh = rhsm;
  goto L620;
  L580: newq = dls001_3.nq - 1;
  rh = rhdn;
  if (dls001_3.kflag < 0 && rh > 1.)
  {
    rh = 1.;
  }
  goto L620;
  L590: newq = dls001_3.l;
  rh = rhup;
  if (rh < 1.1)
  {
    goto L610;
  }
  r__ = dls001_3.el[dls001_3.l - 1] / dls001_3.l;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L600: */
    yh[i__ + (newq + 1) *yh_dim1] = acor[i__] *r__;
  }
  goto L630;
  L610: dls001_3.ialth = 3;
  goto L700;
  L620: if (dls001_3.kflag == 0 && rh < 1.1)
  {
    goto L610;
  }
  if (dls001_3.kflag <=  - 2)
  {
    rh = min(rh, .2);
  }
  /* ----------------------------------------------------------------------- */
  /* If there is a change of order, reset NQ, l, and the coefficients. */
  /* In any case H is reset according to RH and the YH array is rescaled. */
  /* Then exit from 690 if the step was OK, or redo the step otherwise. */
  /* ----------------------------------------------------------------------- */
  if (newq == dls001_3.nq)
  {
    goto L170;
  }
  L630: dls001_3.nq = newq;
  dls001_3.l = dls001_3.nq + 1;
  iret = 2;
  goto L150;
  /* ----------------------------------------------------------------------- */
  /* Control reaches this section if 3 or more failures have occured. */
  /* If 10 failures have occurred, exit with KFLAG = -1. */
  /* It is assumed that the derivatives that have accumulated in the */
  /* YH array have errors of the wrong order.  Hence the first */
  /* derivative is recomputed, and the order is set to 1.  Then */
  /* H is reduced by a factor of 10, and the step is retried, */
  /* until it succeeds or H reaches HMIN. */
  /* ----------------------------------------------------------------------- */
  L640: if (dls001_3.kflag ==  - 10)
  {
    goto L660;
  }
  rh = .1;
  /* Computing MAX */
  d__1 = dls001_3.hmin / abs(dls001_3.h__);
  rh = max(d__1, rh);
  dls001_3.h__ *= rh;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L645: */
    y[i__] = yh[i__ + yh_dim1];
  }
  (*f)(&neq[1], &dls001_3.tn, &y[1], &savf[1]);
  ++dls001_3.nfe;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L650: */
    yh[i__ + (yh_dim1 << 1)] = dls001_3.h__ * savf[i__];
  }
  dls001_3.ipup = dls001_3.miter;
  dls001_3.ialth = 5;
  if (dls001_3.nq == 1)
  {
    goto L200;
  }
  dls001_3.nq = 1;
  dls001_3.l = 2;
  iret = 3;
  goto L150;
  /* ----------------------------------------------------------------------- */
  /* All returns are made through this section.  H is saved in HOLD */
  /* to allow the caller to change H on the next step. */
  /* ----------------------------------------------------------------------- */
  L660: dls001_3.kflag =  - 1;
  goto L720;
  L670: dls001_3.kflag =  - 2;
  goto L720;
  L680: dls001_3.kflag =  - 3;
  goto L720;
  L690: dls001_3.rmax = 10.;
  L700: r__ = 1. / dls001_3.tesco[dls001_3.nqu *3-2];
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L710: */
    acor[i__] *= r__;
  }
  L720: dls001_3.hold = dls001_3.h__;
  dls001_3.jstart = 1;
  return 0;
  /* ----------------------- END OF SUBROUTINE DSTODE ---------------------- */
} /* dstode_ */

/* DECK DEWSET */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dewset_
   (integer *n, integer *itol, doublereal *rtol, doublereal *atol, doublereal
   *ycur, doublereal *ewt)
{
  /* System generated locals */
  integer i__1;
  doublereal d__1;

  /* Local variables */
  static integer i__;

  /* ***BEGIN PROLOGUE  DEWSET */
  /* ***SUBSIDIARY */
  /* ***PURPOSE  Set error weight vector. */
  /* ***TYPE      DOUBLE PRECISION (SEWSET-S, DEWSET-D) */
  /* ***AUTHOR  Hindmarsh, Alan C., (LLNL) */
  /* ***DESCRIPTION */

  /*  This subroutine sets the error weight vector EWT according to */
  /*      EWT(i) = RTOL(i)*ABS(YCUR(i)) + ATOL(i),  i = 1,...,N, */
  /*  with the subscript on RTOL and/or ATOL possibly replaced by 1 above, */
  /*  depending on the value of ITOL. */

  /* ***SEE ALSO  DLSODE */
  /* ***ROUTINES CALLED  (NONE) */
  /* ***REVISION HISTORY  (YYMMDD) */
  /*   791129  DATE WRITTEN */
  /*   890501  Modified prologue to SLATEC/LDOC format.  (FNF) */
  /*   890503  Minor cosmetic changes.  (FNF) */
  /*   930809  Renamed to allow single/double precision versions. (ACH) */
  /* ***END PROLOGUE  DEWSET */
  /* **End */

  /* ***FIRST EXECUTABLE STATEMENT  DEWSET */
  /* Parameter adjustments */
  --ewt;
  --ycur;
  --rtol;
  --atol;

  /* Function Body */
  switch (*itol)
  {
    case 1:
      goto L10;
    case 2:
      goto L20;
    case 3:
      goto L30;
    case 4:
      goto L40;
  }
  L10: i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L15: */
    ewt[i__] = rtol[1]*(d__1 = ycur[i__], abs(d__1)) + atol[1];
  }
  return 0;
  L20: i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L25: */
    ewt[i__] = rtol[1]*(d__1 = ycur[i__], abs(d__1)) + atol[i__];
  }
  return 0;
  L30: i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L35: */
    ewt[i__] = rtol[i__]*(d__1 = ycur[i__], abs(d__1)) + atol[1];
  }
  return 0;
  L40: i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L45: */
    ewt[i__] = rtol[i__]*(d__1 = ycur[i__], abs(d__1)) + atol[i__];
  }
  return 0;
  /* ----------------------- END OF SUBROUTINE DEWSET ---------------------- */
} /* dewset_ */

/* DECK DVNORM */
doublereal SmartMathLibrary::OdePack::ManagedOdePackProvider::dvnorm_(integer
  *n, doublereal *v, doublereal *w)
{
  /* System generated locals */
  integer i__1;
  doublereal ret_val, d__1;

  /* Local variables */
  static integer i__;
  static doublereal sum;

  /* ***BEGIN PROLOGUE  DVNORM */
  /* ***SUBSIDIARY */
  /* ***PURPOSE  Weighted root-mean-square vector norm. */
  /* ***TYPE      DOUBLE PRECISION (SVNORM-S, DVNORM-D) */
  /* ***AUTHOR  Hindmarsh, Alan C., (LLNL) */
  /* ***DESCRIPTION */

  /*  This function routine computes the weighted root-mean-square norm */
  /*  of the vector of length N contained in the array V, with weights */
  /*  contained in the array W of length N: */
  /*    DVNORM = SQRT( (1/N) * SUM( V(i)*W(i) )**2 ) */

  /* ***SEE ALSO  DLSODE */
  /* ***ROUTINES CALLED  (NONE) */
  /* ***REVISION HISTORY  (YYMMDD) */
  /*   791129  DATE WRITTEN */
  /*   890501  Modified prologue to SLATEC/LDOC format.  (FNF) */
  /*   890503  Minor cosmetic changes.  (FNF) */
  /*   930809  Renamed to allow single/double precision versions. (ACH) */
  /* ***END PROLOGUE  DVNORM */
  /* **End */

  /* ***FIRST EXECUTABLE STATEMENT  DVNORM */
  /* Parameter adjustments */
  --w;
  --v;

  /* Function Body */
  sum = 0.;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    /* Computing 2nd power */
    d__1 = v[i__] *w[i__];
    sum += d__1 * d__1;
  }
  ret_val = sqrt(sum /  *n);
  return ret_val;
  /* ----------------------- END OF FUNCTION DVNORM ------------------------ */
} /* dvnorm_ */

/* DECK DIPREP */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::diprep_
   (integer *neq, doublereal *y, doublereal *rwork, integer *ia, integer *ja,
   integer *ipflag, U_fp f, U_fp jac)
{
  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__, imax, lyhd, lyhn;
  static integer lewtn;

  /* ----------------------------------------------------------------------- */
  /* This routine serves as an interface between the driver and */
  /* Subroutine DPREP.  It is called only if MITER is 1 or 2. */
  /* Tasks performed here are: */
  /*  * call DPREP, */
  /*  * reset the required WM segment length LENWK, */
  /*  * move YH back to its final location (following WM in RWORK), */
  /*  * reset pointers for YH, SAVF, EWT, and ACOR, and */
  /*  * move EWT to its new position if ISTATE = 1. */
  /* IPFLAG is an output error indication flag.  IPFLAG = 0 if there was */
  /* no trouble, and IPFLAG is the value of the DPREP error flag IPPER */
  /* if there was trouble in Subroutine DPREP. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --ja;
  --ia;
  --rwork;
  --y;
  --neq;

  /* Function Body */
  *ipflag = 0;
  /* Call DPREP to do matrix preprocessing operations. -------------------- */
  dprep_(&neq[1], &y[1], &rwork[dls001_1.lyh], &rwork[dls001_1.lsavf],
    &rwork[dls001_1.lewt], &rwork[dls001_1.lacor], &ia[1], &ja[1],
    &rwork[dls001_1.lwm], (integer*) &rwork[dls001_1.lwm], ipflag, (U_fp)f, 
    (U_fp)jac);
  dlss01_1.lenwk = max(dlss01_1.lreq, dlss01_1.lwmin);
  if (*ipflag < 0)
  {
    return 0;
  }
  /* If DPREP was successful, move YH to end of required space for WM. ---- */
  lyhn = dls001_1.lwm + dlss01_1.lenwk;
  if (lyhn > dls001_1.lyh)
  {
    return 0;
  }
  lyhd = dls001_1.lyh - lyhn;
  if (lyhd == 0)
  {
    goto L20;
  }
  imax = lyhn - 1+dlss01_1.lenyhm;
  i__1 = imax;
  for (i__ = lyhn; i__ <= i__1; ++i__)
  {
    /* L10: */
    rwork[i__] = rwork[i__ + lyhd];
  }
  dls001_1.lyh = lyhn;
  /* Reset pointers for SAVF, EWT, and ACOR. ------------------------------ */
  L20: dls001_1.lsavf = dls001_1.lyh + dlss01_1.lenyh;
  lewtn = dls001_1.lsavf + dls001_1.n;
  dls001_1.lacor = lewtn + dls001_1.n;
  if (dlss01_1.istatc == 3)
  {
    goto L40;
  }
  /* If ISTATE = 1, move EWT (left) to its new position. ------------------ */
  if (lewtn > dls001_1.lewt)
  {
    return 0;
  }
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L30: */
    rwork[i__ + lewtn - 1] = rwork[i__ + dls001_1.lewt - 1];
  }
  L40: dls001_1.lewt = lewtn;
  return 0;
  /* ----------------------- End of Subroutine DIPREP ---------------------- */
} /* diprep_ */

/* DECK DPREP */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dprep_
   (integer *neq, doublereal *y, doublereal *yh, doublereal *savf, doublereal
   *ewt, doublereal *ftem, integer *ia, integer *ja, doublereal *wk, integer
   *iwk, integer *ipper, S_fp f, S_fp jac)
{
  /* System generated locals */
  integer i__1, i__2;
  doublereal d__1;

  /* Local variables */
  static integer i__, j, k;
  static doublereal dq, yj;
  static integer np1;
  static doublereal fac;
  static integer ibr, ier;
  static doublereal dyj;
  static integer ldif, ipil, ipiu, knew, kmax, kmin, liwk, maxg;
  static doublereal erwt;
  static integer iptt1, iptt2;
  static integer nzsut, lenigp, jfound;

  /* ----------------------------------------------------------------------- */
  /* This routine performs preprocessing related to the sparse linear */
  /* systems that must be solved if MITER = 1 or 2. */
  /* The operations that are performed here are: */
  /*  * compute sparseness structure of Jacobian according to MOSS, */
  /*  * compute grouping of column indices (MITER = 2), */
  /*  * compute a new ordering of rows and columns of the matrix, */
  /*  * reorder JA corresponding to the new ordering, */
  /*  * perform a symbolic LU factorization of the matrix, and */
  /*  * set pointers for segments of the IWK/WK array. */
  /* In addition to variables described previously, DPREP uses the */
  /* following for communication: */
  /* YH     = the history array.  Only the first column, containing the */
  /*          current Y vector, is used.  Used only if MOSS .ne. 0. */
  /* SAVF   = a work array of length NEQ, used only if MOSS .ne. 0. */
  /* EWT    = array of length NEQ containing (inverted) error weights. */
  /*          Used only if MOSS = 2 or if ISTATE = MOSS = 1. */
  /* FTEM   = a work array of length NEQ, identical to ACOR in the driver, */
  /*          used only if MOSS = 2. */
  /* WK     = a real work array of length LENWK, identical to WM in */
  /*          the driver. */
  /* IWK    = integer work array, assumed to occupy the same space as WK. */
  /* LENWK  = the length of the work arrays WK and IWK. */
  /* ISTATC = a copy of the driver input argument ISTATE (= 1 on the */
  /*          first call, = 3 on a continuation call). */
  /* IYS    = flag value from ODRV or CDRV. */
  /* IPPER  = output error flag with the following values and meanings: */
  /*          0  no error. */
  /*         -1  insufficient storage for internal structure pointers. */
  /*         -2  insufficient storage for JGROUP. */
  /*         -3  insufficient storage for ODRV. */
  /*         -4  other error flag from ODRV (should never occur). */
  /*         -5  insufficient storage for CDRV. */
  /*         -6  other error flag from CDRV. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --iwk;
  --wk;
  --ja;
  --ia;
  --ftem;
  --ewt;
  --savf;
  --yh;
  --y;
  --neq;

  /* Function Body */
  dlss01_2.ibian = dlss01_2.lrat << 1;
  dlss01_2.ipian = dlss01_2.ibian + 1;
  np1 = dls001_1.n + 1;
  dlss01_2.ipjan = dlss01_2.ipian + np1;
  dlss01_2.ibjan = dlss01_2.ipjan - 1;
  liwk = dlss01_2.lenwk * dlss01_2.lrat;
  if (dlss01_2.ipjan + dls001_1.n - 1 > liwk)
  {
    goto L210;
  }
  if (dlss01_2.moss == 0)
  {
    goto L30;
  }

  if (dlss01_2.istatc == 3)
  {
    goto L20;
  }
  /* ISTATE = 1 and MOSS .ne. 0.  Perturb Y for structure determination. -- */
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    erwt = 1. / ewt[i__];
    fac = 1. / (i__ + 1.) + 1.;
    y[i__] += fac * d_sign(&erwt, &y[i__]);
    /* L10: */
  }
  switch (dlss01_2.moss)
  {
    case 1:
      goto L70;
    case 2:
      goto L100;
  }

  L20: 
  /* ISTATE = 3 and MOSS .ne. 0.  Load Y from YH(*,1). -------------------- */
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L25: */
    y[i__] = yh[i__];
  }
  switch (dlss01_2.moss)
  {
    case 1:
      goto L70;
    case 2:
      goto L100;
  }

  /* MOSS = 0.  Process user's IA,JA.  Add diagonal entries if necessary. - */
  L30: knew = dlss01_2.ipjan;
  kmin = ia[1];
  iwk[dlss01_2.ipian] = 1;
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    jfound = 0;
    kmax = ia[j + 1] - 1;
    if (kmin > kmax)
    {
      goto L45;
    }
    i__2 = kmax;
    for (k = kmin; k <= i__2; ++k)
    {
      i__ = ja[k];
      if (i__ == j)
      {
        jfound = 1;
      }
      if (knew > liwk)
      {
        goto L210;
      }
      iwk[knew] = i__;
      ++knew;
      /* L40: */
    }
    if (jfound == 1)
    {
      goto L50;
    }
    L45: if (knew > liwk)
    {
      goto L210;
    }
    iwk[knew] = j;
    ++knew;
    L50: iwk[dlss01_2.ipian + j] = knew + 1-dlss01_2.ipjan;
    kmin = kmax + 1;
    /* L60: */
  }
  goto L140;

  /* MOSS = 1.  Compute structure from user-supplied Jacobian routine JAC. */
  L70: 
  /* A dummy call to F allows user to create temporaries for use in JAC. -- */
  (*f)(&neq[1], &dls001_1.tn, &y[1], &savf[1]);
  k = dlss01_2.ipjan;
  iwk[dlss01_2.ipian] = 1;
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    if (k > liwk)
    {
      goto L210;
    }
    iwk[k] = j;
    ++k;
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L75: */
      savf[i__] = 0.;
    }
    (*jac)(&neq[1], &dls001_1.tn, &y[1], &j, &iwk[dlss01_2.ipian],
      &iwk[dlss01_2.ipjan], &savf[1]);
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      if ((d__1 = savf[i__], abs(d__1)) <= dlss01_2.seth)
      {
        goto L80;
      }
      if (i__ == j)
      {
        goto L80;
      }
      if (k > liwk)
      {
        goto L210;
      }
      iwk[k] = i__;
      ++k;
      L80: ;
    }
    iwk[dlss01_2.ipian + j] = k + 1-dlss01_2.ipjan;
    /* L90: */
  }
  goto L140;

  /* MOSS = 2.  Compute structure from results of N + 1 calls to F. ------- */
  L100: k = dlss01_2.ipjan;
  iwk[dlss01_2.ipian] = 1;
  (*f)(&neq[1], &dls001_1.tn, &y[1], &savf[1]);
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    if (k > liwk)
    {
      goto L210;
    }
    iwk[k] = j;
    ++k;
    yj = y[j];
    erwt = 1. / ewt[j];
    dyj = d_sign(&erwt, &yj);
    y[j] = yj + dyj;
    (*f)(&neq[1], &dls001_1.tn, &y[1], &ftem[1]);
    y[j] = yj;
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      dq = (ftem[i__] - savf[i__]) / dyj;
      if (abs(dq) <= dlss01_2.seth)
      {
        goto L110;
      }
      if (i__ == j)
      {
        goto L110;
      }
      if (k > liwk)
      {
        goto L210;
      }
      iwk[k] = i__;
      ++k;
      L110: ;
    }
    iwk[dlss01_2.ipian + j] = k + 1-dlss01_2.ipjan;
    /* L120: */
  }

  L140: if (dlss01_2.moss == 0 || dlss01_2.istatc != 1)
  {
    goto L150;
  }
  /* If ISTATE = 1 and MOSS .ne. 0, restore Y from YH. -------------------- */
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L145: */
    y[i__] = yh[i__];
  }
  L150: dlss01_2.nnz = iwk[dlss01_2.ipian + dls001_1.n] - 1;
  lenigp = 0;
  dlss01_2.ipigp = dlss01_2.ipjan + dlss01_2.nnz;
  if (dls001_1.miter != 2)
  {
    goto L160;
  }

  /* Compute grouping of column indices (MITER = 2). ---------------------- */
  maxg = np1;
  dlss01_2.ipjgp = dlss01_2.ipjan + dlss01_2.nnz;
  dlss01_2.ibjgp = dlss01_2.ipjgp - 1;
  dlss01_2.ipigp = dlss01_2.ipjgp + dls001_1.n;
  iptt1 = dlss01_2.ipigp + np1;
  iptt2 = iptt1 + dls001_1.n;
  dlss01_2.lreq = iptt2 + dls001_1.n - 1;
  if (dlss01_2.lreq > liwk)
  {
    goto L220;
  }
  jgroup_(&dls001_1.n, &iwk[dlss01_2.ipian], &iwk[dlss01_2.ipjan], &maxg,
    &dlss01_2.ngp, &iwk[dlss01_2.ipigp], &iwk[dlss01_2.ipjgp], &iwk[iptt1],
    &iwk[iptt2], &ier);
  if (ier != 0)
  {
    goto L220;
  }
  lenigp = dlss01_2.ngp + 1;

  /* Compute new ordering of rows/columns of Jacobian. -------------------- */
  L160: dlss01_2.ipr = dlss01_2.ipigp + lenigp;
  dlss01_2.ipc = dlss01_2.ipr;
  dlss01_2.ipic = dlss01_2.ipc + dls001_1.n;
  dlss01_2.ipisp = dlss01_2.ipic + dls001_1.n;
  dlss01_2.iprsp = (dlss01_2.ipisp - 2) / dlss01_2.lrat + 2;
  dlss01_2.iesp = dlss01_2.lenwk + 1-dlss01_2.iprsp;
  if (dlss01_2.iesp < 0)
  {
    goto L230;
  }
  ibr = dlss01_2.ipr - 1;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L170: */
    iwk[ibr + i__] = i__;
  }
  dlss01_2.nsp = liwk + 1-dlss01_2.ipisp;
  odrv_(&dls001_1.n, &iwk[dlss01_2.ipian], &iwk[dlss01_2.ipjan], &wk[1],
    &iwk[dlss01_2.ipr], &iwk[dlss01_2.ipic], &dlss01_2.nsp,
    &iwk[dlss01_2.ipisp], &c__1, &dlss01_2.iys);
  if (dlss01_2.iys == dls001_1.n *11+1)
  {
    goto L240;
  }
  if (dlss01_2.iys != 0)
  {
    goto L230;
  }

  /* Reorder JAN and do symbolic LU factorization of matrix. -------------- */
  dlss01_2.ipa = dlss01_2.lenwk + 1-dlss01_2.nnz;
  dlss01_2.nsp = dlss01_2.ipa - dlss01_2.iprsp;
  /* Computing MAX */
  i__1 = dls001_1.n * 12 / dlss01_2.lrat, i__2 = dls001_1.n * 6 / dlss01_2.lrat
    + (dls001_1.n << 1) + dlss01_2.nnz;
  dlss01_2.lreq = max(i__1, i__2) + 3;
  dlss01_2.lreq = dlss01_2.lreq + dlss01_2.iprsp - 1+dlss01_2.nnz;
  if (dlss01_2.lreq > dlss01_2.lenwk)
  {
    goto L250;
  }
  dlss01_2.iba = dlss01_2.ipa - 1;
  i__1 = dlss01_2.nnz;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L180: */
    wk[dlss01_2.iba + i__] = 0.;
  }
  dlss01_2.ipisp = dlss01_2.lrat *(dlss01_2.iprsp - 1) + 1;
  cdrv_(&dls001_1.n, &iwk[dlss01_2.ipr], &iwk[dlss01_2.ipc],
    &iwk[dlss01_2.ipic], &iwk[dlss01_2.ipian], &iwk[dlss01_2.ipjan],
    &wk[dlss01_2.ipa], &wk[dlss01_2.ipa], &wk[dlss01_2.ipa], &dlss01_2.nsp,
    &iwk[dlss01_2.ipisp], &wk[dlss01_2.iprsp], &dlss01_2.iesp, &c__5,
    &dlss01_2.iys);
  dlss01_2.lreq = dlss01_2.lenwk - dlss01_2.iesp;
  if (dlss01_2.iys == dls001_1.n *10+1)
  {
    goto L250;
  }
  if (dlss01_2.iys != 0)
  {
    goto L260;
  }
  ipil = dlss01_2.ipisp;
  ipiu = ipil + (dls001_1.n << 1) + 1;
  dlss01_2.nzu = iwk[ipil + dls001_1.n] - iwk[ipil];
  dlss01_2.nzl = iwk[ipiu + dls001_1.n] - iwk[ipiu];
  if (dlss01_2.lrat > 1)
  {
    goto L190;
  }
  adjlr_(&dls001_1.n, &iwk[dlss01_2.ipisp], &ldif);
  dlss01_2.lreq += ldif;
  L190: if (dlss01_2.lrat == 2 && dlss01_2.nnz == dls001_1.n)
  {
    ++dlss01_2.lreq;
  }
  dlss01_2.nsp = dlss01_2.nsp + dlss01_2.lreq - dlss01_2.lenwk;
  dlss01_2.ipa = dlss01_2.lreq + 1-dlss01_2.nnz;
  dlss01_2.iba = dlss01_2.ipa - 1;
  *ipper = 0;
  return 0;

  L210: *ipper =  - 1;
  dlss01_2.lreq = ((dls001_1.n << 1) + 1) / dlss01_2.lrat + 2;
  /* Computing MAX */
  i__1 = dlss01_2.lenwk + 1;
  dlss01_2.lreq = max(i__1, dlss01_2.lreq);
  return 0;

  L220: *ipper =  - 2;
  dlss01_2.lreq = (dlss01_2.lreq - 1) / dlss01_2.lrat + 1;
  return 0;

  L230: *ipper =  - 3;
  cntnzu_(&dls001_1.n, &iwk[dlss01_2.ipian], &iwk[dlss01_2.ipjan], &nzsut);
  dlss01_2.lreq = dlss01_2.lenwk - dlss01_2.iesp + (dls001_1.n *3+(nzsut << 2) 
    - 1) / dlss01_2.lrat + 1;
  return 0;

  L240: *ipper =  - 4;
  return 0;

  L250: *ipper =  - 5;
  return 0;

  L260: *ipper =  - 6;
  dlss01_2.lreq = dlss01_2.lenwk;
  return 0;
  /* ----------------------- End of Subroutine DPREP ----------------------- */
} /* dprep_ */

/* DECK DPRJS */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dprjs_
   (integer *neq, doublereal *y, doublereal *yh, integer *nyh, doublereal *ewt,
   doublereal *ftem, doublereal *savf, doublereal *wk, integer *iwk, S_fp f,
   S_fp jac)
{
  /* System generated locals */
  integer yh_dim1, yh_offset, i__1, i__2, i__3;
  doublereal d__1, d__2, d__3;

  /* Local variables */
  static integer i__, j, k;
  static doublereal r__, r0, di;
  static integer jj, ng;
  static doublereal hl0, fac, con, pij;
  static integer jok, imul, jmax, jmin, kmax, kmin;
  static doublereal rcon;
  static doublereal srur, rcont;

  /* ----------------------------------------------------------------------- */
  /* DPRJS is called to compute and process the matrix */
  /* P = I - H*EL(1)*J , where J is an approximation to the Jacobian. */
  /* J is computed by columns, either by the user-supplied routine JAC */
  /* if MITER = 1, or by finite differencing if MITER = 2. */
  /* if MITER = 3, a diagonal approximation to J is used. */
  /* if MITER = 1 or 2, and if the existing value of the Jacobian */
  /* (as contained in P) is considered acceptable, then a new value of */
  /* P is reconstructed from the old value.  In any case, when MITER */
  /* is 1 or 2, the P matrix is subjected to LU decomposition in CDRV. */
  /* P and its LU decomposition are stored (separately) in WK. */

  /* In addition to variables described previously, communication */
  /* with DPRJS uses the following: */
  /* Y     = array containing predicted values on entry. */
  /* FTEM  = work array of length N (ACOR in DSTODE). */
  /* SAVF  = array containing f evaluated at predicted y. */
  /* WK    = real work space for matrices.  On output it contains the */
  /*         inverse diagonal matrix if MITER = 3, and P and its sparse */
  /*         LU decomposition if MITER is 1 or 2. */
  /*         Storage of matrix elements starts at WK(3). */
  /*         WK also contains the following matrix-related data: */
  /*         WK(1) = SQRT(UROUND), used in numerical Jacobian increments. */
  /*         WK(2) = H*EL0, saved for later use if MITER = 3. */
  /* IWK   = integer work space for matrix-related data, assumed to */
  /*         be equivalenced to WK.  In addition, WK(IPRSP) and IWK(IPISP) */
  /*         are assumed to have identical locations. */
  /* EL0   = EL(1) (input). */
  /* IERPJ = output error flag (in Common). */
  /*       = 0 if no error. */
  /*       = 1  if zero pivot found in CDRV. */
  /*       = 2  if a singular matrix arose with MITER = 3. */
  /*       = -1 if insufficient storage for CDRV (should not occur here). */
  /*       = -2 if other error found in CDRV (should not occur here). */
  /* JCUR  = output flag showing status of (approximate) Jacobian matrix: */
  /*          = 1 to indicate that the Jacobian is now current, or */
  /*          = 0 to indicate that a saved value was used. */
  /* This routine also uses other variables in Common. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --neq;
  --y;
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --ewt;
  --ftem;
  --savf;
  --wk;
  --iwk;

  /* Function Body */
  hl0 = dls001_1.h__ * dls001_1.el0;
  con =  - hl0;
  if (dls001_1.miter == 3)
  {
    goto L300;
  }
  /* See whether J should be reevaluated (JOK = 0) or not (JOK = 1). ------ */
  jok = 1;
  if (dls001_1.nst == 0 || dls001_1.nst >= dlss01_2.nslj + dlss01_2.msbj)
  {
    jok = 0;
  }
  if (dls001_1.icf == 1 && (d__1 = dls001_1.rc - 1., abs(d__1)) <
    dlss01_2.ccmxj)
  {
    jok = 0;
  }
  if (dls001_1.icf == 2)
  {
    jok = 0;
  }
  if (jok == 1)
  {
    goto L250;
  }

  /* MITER = 1 or 2, and the Jacobian is to be reevaluated. --------------- */
  L20: dls001_1.jcur = 1;
  ++dls001_1.nje;
  dlss01_2.nslj = dls001_1.nst;
  dlss01_2.iplost = 0;
  dlss01_2.conmin = abs(con);
  switch (dls001_1.miter)
  {
    case 1:
      goto L100;
    case 2:
      goto L200;
  }

  /* If MITER = 1, call JAC, multiply by scalar, and add identity. -------- */
  L100: kmin = iwk[dlss01_2.ipian];
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    kmax = iwk[dlss01_2.ipian + j] - 1;
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L110: */
      ftem[i__] = 0.;
    }
    (*jac)(&neq[1], &dls001_1.tn, &y[1], &j, &iwk[dlss01_2.ipian],
      &iwk[dlss01_2.ipjan], &ftem[1]);
    i__2 = kmax;
    for (k = kmin; k <= i__2; ++k)
    {
      i__ = iwk[dlss01_2.ibjan + k];
      wk[dlss01_2.iba + k] = ftem[i__] *con;
      if (i__ == j)
      {
        wk[dlss01_2.iba + k] += 1.;
      }
      /* L120: */
    }
    kmin = kmax + 1;
    /* L130: */
  }
  goto L290;

  /* If MITER = 2, make NGP calls to F to approximate J and P. ------------ */
  L200: fac = dvnorm_(&dls001_1.n, &savf[1], &ewt[1]);
  r0 = abs(dls001_1.h__) *1e3 * dls001_1.uround * dls001_1.n * fac;
  if (r0 == 0.)
  {
    r0 = 1.;
  }
  srur = wk[1];
  jmin = iwk[dlss01_2.ipigp];
  i__1 = dlss01_2.ngp;
  for (ng = 1; ng <= i__1; ++ng)
  {
    jmax = iwk[dlss01_2.ipigp + ng] - 1;
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      jj = iwk[dlss01_2.ibjgp + j];
      /* Computing MAX */
      d__2 = srur *(d__1 = y[jj], abs(d__1)), d__3 = r0 / ewt[jj];
      r__ = max(d__2, d__3);
      /* L210: */
      y[jj] += r__;
    }
    (*f)(&neq[1], &dls001_1.tn, &y[1], &ftem[1]);
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      jj = iwk[dlss01_2.ibjgp + j];
      y[jj] = yh[jj + yh_dim1];
      /* Computing MAX */
      d__2 = srur *(d__1 = y[jj], abs(d__1)), d__3 = r0 / ewt[jj];
      r__ = max(d__2, d__3);
      fac =  - hl0 / r__;
      kmin = iwk[dlss01_2.ibian + jj];
      kmax = iwk[dlss01_2.ibian + jj + 1] - 1;
      i__3 = kmax;
      for (k = kmin; k <= i__3; ++k)
      {
        i__ = iwk[dlss01_2.ibjan + k];
        wk[dlss01_2.iba + k] = (ftem[i__] - savf[i__]) *fac;
        if (i__ == jj)
        {
          wk[dlss01_2.iba + k] += 1.;
        }
        /* L220: */
      }
      /* L230: */
    }
    jmin = jmax + 1;
    /* L240: */
  }
  dls001_1.nfe += dlss01_2.ngp;
  goto L290;

  /* If JOK = 1, reconstruct new P from old P. ---------------------------- */
  L250: dls001_1.jcur = 0;
  rcon = con / dlss01_2.con0;
  rcont = abs(con) / dlss01_2.conmin;
  if (rcont > dlss01_2.rbig && dlss01_2.iplost == 1)
  {
    goto L20;
  }
  kmin = iwk[dlss01_2.ipian];
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    kmax = iwk[dlss01_2.ipian + j] - 1;
    i__2 = kmax;
    for (k = kmin; k <= i__2; ++k)
    {
      i__ = iwk[dlss01_2.ibjan + k];
      pij = wk[dlss01_2.iba + k];
      if (i__ != j)
      {
        goto L260;
      }
      pij +=  - 1.;
      if (abs(pij) >= dlss01_2.psmall)
      {
        goto L260;
      }
      dlss01_2.iplost = 1;
      /* Computing MIN */
      d__1 = abs(dlss01_2.con0);
      dlss01_2.conmin = min(d__1, dlss01_2.conmin);
      L260: pij *= rcon;
      if (i__ == j)
      {
        pij += 1.;
      }
      wk[dlss01_2.iba + k] = pij;
      /* L270: */
    }
    kmin = kmax + 1;
    /* L275: */
  }

  /* Do numerical factorization of P matrix. ------------------------------ */
  L290: ++dlss01_2.nlu;
  dlss01_2.con0 = con;
  dls001_1.ierpj = 0;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L295: */
    ftem[i__] = 0.;
  }
  cdrv_(&dls001_1.n, &iwk[dlss01_2.ipr], &iwk[dlss01_2.ipc],
    &iwk[dlss01_2.ipic], &iwk[dlss01_2.ipian], &iwk[dlss01_2.ipjan],
    &wk[dlss01_2.ipa], &ftem[1], &ftem[1], &dlss01_2.nsp, &iwk[dlss01_2.ipisp],
    &wk[dlss01_2.iprsp], &dlss01_2.iesp, &c__2, &dlss01_2.iys);
  if (dlss01_2.iys == 0)
  {
    return 0;
  }
  imul = (dlss01_2.iys - 1) / dls001_1.n;
  dls001_1.ierpj =  - 2;
  if (imul == 8)
  {
    dls001_1.ierpj = 1;
  }
  if (imul == 10)
  {
    dls001_1.ierpj =  - 1;
  }
  return 0;

  /* If MITER = 3, construct a diagonal approximation to J and P. --------- */
  L300: dls001_1.jcur = 1;
  ++dls001_1.nje;
  wk[2] = hl0;
  dls001_1.ierpj = 0;
  r__ = dls001_1.el0 * .1;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L310: */
    y[i__] += r__ *(dls001_1.h__ *savf[i__] - yh[i__ + (yh_dim1 << 1)]);
  }
  (*f)(&neq[1], &dls001_1.tn, &y[1], &wk[3]);
  ++dls001_1.nfe;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    r0 = dls001_1.h__ * savf[i__] - yh[i__ + (yh_dim1 << 1)];
    di = r0 * .1 - dls001_1.h__ *(wk[i__ + 2] - savf[i__]);
    wk[i__ + 2] = 1.;
    if (abs(r0) < dls001_1.uround / ewt[i__])
    {
      goto L320;
    }
    if (abs(di) == 0.)
    {
      goto L330;
    }
    wk[i__ + 2] = r0 * .1 / di;
    L320: ;
  }
  return 0;
  L330: dls001_1.ierpj = 2;
  return 0;
  /* ----------------------- End of Subroutine DPRJS ----------------------- */
} /* dprjs_ */

/* DECK DSOLSS */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dsolss_
   (doublereal *wk, integer *iwk, doublereal *x, doublereal *tem)
{
  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__;
  static doublereal r__, di, hl0, phl0;

  /* ----------------------------------------------------------------------- */
  /* This routine manages the solution of the linear system arising from */
  /* a chord iteration.  It is called if MITER .ne. 0. */
  /* If MITER is 1 or 2, it calls CDRV to accomplish this. */
  /* If MITER = 3 it updates the coefficient H*EL0 in the diagonal */
  /* matrix, and then computes the solution. */
  /* communication with DSOLSS uses the following variables: */
  /* WK    = real work space containing the inverse diagonal matrix if */
  /*         MITER = 3 and the LU decomposition of the matrix otherwise. */
  /*         Storage of matrix elements starts at WK(3). */
  /*         WK also contains the following matrix-related data: */
  /*         WK(1) = SQRT(UROUND) (not used here), */
  /*         WK(2) = HL0, the previous value of H*EL0, used if MITER = 3. */
  /* IWK   = integer work space for matrix-related data, assumed to */
  /*         be equivalenced to WK.  In addition, WK(IPRSP) and IWK(IPISP) */
  /*         are assumed to have identical locations. */
  /* X     = the right-hand side vector on input, and the solution vector */
  /*         on output, of length N. */
  /* TEM   = vector of work space of length N, not used in this version. */
  /* IERSL = output flag (in Common). */
  /*         IERSL = 0  if no trouble occurred. */
  /*         IERSL = -1 if CDRV returned an error flag (MITER = 1 or 2). */
  /*                    This should never occur and is considered fatal. */
  /*         IERSL = 1  if a singular matrix arose with MITER = 3. */
  /* This routine also uses other variables in Common. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --tem;
  --x;
  --iwk;
  --wk;

  /* Function Body */
  dls001_1.iersl = 0;
  switch (dls001_1.miter)
  {
    case 1:
      goto L100;
    case 2:
      goto L100;
    case 3:
      goto L300;
  }
  L100: cdrv_(&dls001_1.n, &iwk[dlss01_1.ipr], &iwk[dlss01_1.ipc],
    &iwk[dlss01_1.ipic], &iwk[dlss01_1.ipian], &iwk[dlss01_1.ipjan],
    &wk[dlss01_1.ipa], &x[1], &x[1], &dlss01_1.nsp, &iwk[dlss01_1.ipisp], 
    &wk[dlss01_1.iprsp], &dlss01_1.iesp, &c__4, &dls001_1.iersl);
  if (dls001_1.iersl != 0)
  {
    dls001_1.iersl =  - 1;
  }
  return 0;

  L300: phl0 = wk[2];
  hl0 = dls001_1.h__ * dls001_1.el0;
  wk[2] = hl0;
  if (hl0 == phl0)
  {
    goto L330;
  }
  r__ = hl0 / phl0;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    di = 1. - r__ *(1. - 1. / wk[i__ + 2]);
    if (abs(di) == 0.)
    {
      goto L390;
    }
    /* L320: */
    wk[i__ + 2] = 1. / di;
  }
  L330: i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L340: */
    x[i__] = wk[i__ + 2] *x[i__];
  }
  return 0;
  L390: dls001_1.iersl = 1;
  return 0;

  /* ----------------------- End of Subroutine DSOLSS ---------------------- */
} /* dsolss_ */

/* DECK DSRCMS */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dsrcms_
   (doublereal *rsav, integer *isav, integer *job)
{
  /* Initialized data */

  static integer lenrls = 218;
  static integer lenils = 37;
  static integer lenrss = 6;
  static integer leniss = 34;

  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__;

  /* ----------------------------------------------------------------------- */
  /* This routine saves or restores (depending on JOB) the contents of */
  /* the Common blocks DLS001, DLSS01, which are used */
  /* internally by one or more ODEPACK solvers. */

  /* RSAV = real array of length 224 or more. */
  /* ISAV = integer array of length 71 or more. */
  /* JOB  = flag indicating to save or restore the Common blocks: */
  /*        JOB  = 1 if Common is to be saved (written to RSAV/ISAV) */
  /*        JOB  = 2 if Common is to be restored (read from RSAV/ISAV) */
  /*        A call with JOB = 2 presumes a prior call with JOB = 1. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --isav;
  --rsav;

  /* Function Body */

  if (*job == 2)
  {
    goto L100;
  }
  i__1 = lenrls;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    rsav[i__] = dls001_2.rls[i__ - 1];
  }
  i__1 = lenrss;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L15: */
    rsav[lenrls + i__] = dlss01_3.rlss[i__ - 1];
  }

  i__1 = lenils;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L20: */
    isav[i__] = dls001_2.ils[i__ - 1];
  }
  i__1 = leniss;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L25: */
    isav[lenils + i__] = dlss01_3.ilss[i__ - 1];
  }

  return 0;

  L100: i__1 = lenrls;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L110: */
    dls001_2.rls[i__ - 1] = rsav[i__];
  }
  i__1 = lenrss;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L115: */
    dlss01_3.rlss[i__ - 1] = rsav[lenrls + i__];
  }

  i__1 = lenils;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L120: */
    dls001_2.ils[i__ - 1] = isav[i__];
  }
  i__1 = leniss;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L125: */
    dlss01_3.ilss[i__ - 1] = isav[lenils + i__];
  }

  return 0;
  /* ----------------------- End of Subroutine DSRCMS ---------------------- */
} /* dsrcms_ */

/* DECK ODRV */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::odrv_
   (integer *n, integer *ia, integer *ja, doublereal *a, integer *p, integer
   *ip, integer *nsp, integer *isp, integer *path, integer *flag__)
{
  static integer l, q, v;
  static integer max__, tmp;
  static integer head, next;
  static logical dflag;

  /*                                                                 5/2/83 */
  /* *********************************************************************** */
  /*  odrv -- driver for sparse matrix reordering routines */
  /* *********************************************************************** */

  /*  description */

  /*    odrv finds a minimum degree ordering of the rows and columns */
  /*    of a matrix m stored in (ia,ja,a) format (see below).  for the */
  /*    reordered matrix, the work and storage required to perform */
  /*    gaussian elimination is (usually) significantly less. */

  /*    note.. odrv and its subordinate routines have been modified to */
  /*    compute orderings for general matrices, not necessarily having any */
  /*    symmetry.  the miminum degree ordering is computed for the */
  /*    structure of the symmetric matrix  m + m-transpose. */
  /*    modifications to the original odrv module have been made in */
  /*    the coding in subroutine mdi, and in the initial comments in */
  /*    subroutines odrv and md. */

  /*    if only the nonzero entries in the upper triangle of m are being */
  /*    stored, then odrv symmetrically reorders (ia,ja,a), (optionally) */
  /*    with the diagonal entries placed first in each row.  this is to */
  /*    ensure that if m(i,j) will be in the upper triangle of m with */
  /*    respect to the new ordering, then m(i,j) is stored in row i (and */
  /*    thus m(j,i) is not stored),  whereas if m(i,j) will be in the */
  /*    strict lower triangle of m, then m(j,i) is stored in row j (and */
  /*    thus m(i,j) is not stored). */


  /*  storage of sparse matrices */

  /*    the nonzero entries of the matrix m are stored row-by-row in the */
  /*    array a.  to identify the individual nonzero entries in each row, */
  /*    we need to know in which column each entry lies.  these column */
  /*    indices are stored in the array ja.  i.e., if  a(k) = m(i,j),  then */
  /*    ja(k) = j.  to identify the individual rows, we need to know where */
  /*    each row starts.  these row pointers are stored in the array ia. */
  /*    i.e., if m(i,j) is the first nonzero entry (stored) in the i-th row */
  /*    and  a(k) = m(i,j),  then  ia(i) = k.  moreover, ia(n+1) points to */
  /*    the first location following the last element in the last row. */
  /*    thus, the number of entries in the i-th row is  ia(i+1) - ia(i), */
  /*    the nonzero entries in the i-th row are stored consecutively in */

  /*            a(ia(i)),  a(ia(i)+1),  ..., a(ia(i+1)-1), */

  /*    and the corresponding column indices are stored consecutively in */

  /*            ja(ia(i)), ja(ia(i)+1), ..., ja(ia(i+1)-1). */

  /*    when the coefficient matrix is symmetric, only the nonzero entries */
  /*    in the upper triangle need be stored.  for example, the matrix */

  /*             ( 1  0  2  3  0 ) */
  /*             ( 0  4  0  0  0 ) */
  /*         m = ( 2  0  5  6  0 ) */
  /*             ( 3  0  6  7  8 ) */
  /*             ( 0  0  0  8  9 ) */

  /*    could be stored as */

  /*            - 1  2  3  4  5  6  7  8  9 10 11 12 13 */
  /*         ---+-------------------------------------- */
  /*         ia - 1  4  5  8 12 14 */
  /*         ja - 1  3  4  2  1  3  4  1  3  4  5  4  5 */
  /*          a - 1  2  3  4  2  5  6  3  6  7  8  8  9 */

  /*    or (symmetrically) as */

  /*            - 1  2  3  4  5  6  7  8  9 */
  /*         ---+-------------------------- */
  /*         ia - 1  4  5  7  9 10 */
  /*         ja - 1  3  4  2  3  4  4  5  5 */
  /*          a - 1  2  3  4  5  6  7  8  9          . */


  /*  parameters */

  /*    n    - order of the matrix */

  /*    ia   - integer one-dimensional array containing pointers to delimit */
  /*           rows in ja and a.  dimension = n+1 */

  /*    ja   - integer one-dimensional array containing the column indices */
  /*           corresponding to the elements of a.  dimension = number of */
  /*           nonzero entries in (the upper triangle of) m */

  /*    a    - real one-dimensional array containing the nonzero entries in */
  /*           (the upper triangle of) m, stored by rows.  dimension = */
  /*           number of nonzero entries in (the upper triangle of) m */

  /*    p    - integer one-dimensional array used to return the permutation */
  /*           of the rows and columns of m corresponding to the minimum */
  /*           degree ordering.  dimension = n */

  /*    ip   - integer one-dimensional array used to return the inverse of */
  /*           the permutation returned in p.  dimension = n */

  /*    nsp  - declared dimension of the one-dimensional array isp.  nsp */
  /*           must be at least  3n+4k,  where k is the number of nonzeroes */
  /*           in the strict upper triangle of m */

  /*    isp  - integer one-dimensional array used for working storage. */
  /*           dimension = nsp */

  /*    path - integer path specification.  values and their meanings are - */
  /*             1  find minimum degree ordering only */
  /*             2  find minimum degree ordering and reorder symmetrically */
  /*                  stored matrix (used when only the nonzero entries in */
  /*                  the upper triangle of m are being stored) */
  /*             3  reorder symmetrically stored matrix as specified by */
  /*                  input permutation (used when an ordering has already */
  /*                  been determined and only the nonzero entries in the */
  /*                  upper triangle of m are being stored) */
  /*             4  same as 2 but put diagonal entries at start of each row */
  /*             5  same as 3 but put diagonal entries at start of each row */

  /*    flag - integer error flag.  values and their meanings are - */
  /*               0    no errors detected */
  /*              9n+k  insufficient storage in md */
  /*             10n+1  insufficient storage in odrv */
  /*             11n+1  illegal path specification */


  /*  conversion from real to double precision */

  /*    change the real declarations in odrv and sro to double precision */
  /*    declarations. */

  /* ----------------------------------------------------------------------- */

  /* ...  real  a(*) */

  /* ----initialize error flag and validate path specification */
  /* Parameter adjustments */
  --isp;
  --ip;
  --p;
  --a;
  --ja;
  --ia;

  /* Function Body */
  *flag__ = 0;
  if (*path < 1 || 5 <  *path)
  {
    goto L111;
  }

  /* ----allocate storage and find minimum degree ordering */
  if ((*path - 1)*(*path - 2)*(*path - 4) != 0)
  {
    goto L1;
  }
  max__ = (*nsp -  *n) / 2;
  v = 1;
  l = v + max__;
  head = l + max__;
  next = head +  *n;
  if (max__ <  *n)
  {
    goto L110;
  }

  md_(n, &ia[1], &ja[1], &max__, &isp[v], &isp[l], &isp[head], &p[1], &ip[1],
    &isp[v], flag__);
  if (*flag__ != 0)
  {
    goto L100;
  }

  /* ----allocate storage and symmetrically reorder matrix */
  L1: if ((*path - 2)*(*path - 3)*(*path - 4)*(*path - 5) != 0)
  {
    goto L2;
  }
  tmp =  *nsp + 1- *n;
  q = tmp - (ia[ *n + 1] - 1);
  if (q < 1)
  {
    goto L110;
  }

  dflag =  *path == 4 ||  *path == 5;
  sro_(n, &ip[1], &ia[1], &ja[1], &a[1], &isp[tmp], &isp[q], &dflag);

  L2: return 0;

  /* ** error -- error detected in md */
  L100: return 0;
  /* ** error -- insufficient storage */
  L110: *flag__ =  *n * 10+1;
  return 0;
  /* ** error -- illegal path specified */
  L111: *flag__ =  *n * 11+1;
  return 0;
} /* odrv_ */

#undef vk
#undef ek

 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::sro_
   (integer *n, integer *ip, integer *ia, integer *ja, doublereal *a, integer
   *q, integer *r__, logical *dflag)
{
  /* System generated locals */
  integer i__1, i__2;

  /* Local variables */
  static integer i__, j, k;
  static doublereal ak;
  static integer jak, jmin, jmax, ilast, jdummy;

  /* *********************************************************************** */
  /*  sro -- symmetric reordering of sparse symmetric matrix */
  /* *********************************************************************** */

  /*  description */

  /*    the nonzero entries of the matrix m are assumed to be stored */
  /*    symmetrically in (ia,ja,a) format (i.e., not both m(i,j) and m(j,i) */
  /*    are stored if i ne j). */

  /*    sro does not rearrange the order of the rows, but does move */
  /*    nonzeroes from one row to another to ensure that if m(i,j) will be */
  /*    in the upper triangle of m with respect to the new ordering, then */
  /*    m(i,j) is stored in row i (and thus m(j,i) is not stored),  whereas */
  /*    if m(i,j) will be in the strict lower triangle of m, then m(j,i) is */
  /*    stored in row j (and thus m(i,j) is not stored). */


  /*  additional parameters */

  /*    q     - integer one-dimensional work array.  dimension = n */

  /*    r     - integer one-dimensional work array.  dimension = number of */
  /*            nonzero entries in the upper triangle of m */

  /*    dflag - logical variable.  if dflag = .true., then store nonzero */
  /*            diagonal elements at the beginning of the row */

  /* ----------------------------------------------------------------------- */

  /* ...  real  a(*),  ak */


  /* --phase 1 -- find row in which to store each nonzero */
  /* ----initialize count of nonzeroes to be stored in each row */
  /* Parameter adjustments */
  --r__;
  --q;
  --a;
  --ja;
  --ia;
  --ip;

  /* Function Body */
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L1: */
    q[i__] = 0;
  }

  /* ----for each nonzero element a(j) */
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    jmin = ia[i__];
    jmax = ia[i__ + 1] - 1;
    if (jmin > jmax)
    {
      goto L3;
    }
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {

      /* --------find row (=r(j)) and column (=ja(j)) in which to store a(j) ... */
      k = ja[j];
      if (ip[k] < ip[i__])
      {
        ja[j] = i__;
      }
      if (ip[k] >= ip[i__])
      {
        k = i__;
      }
      r__[j] = k;

      /* --------... and increment count of nonzeroes (=q(r(j)) in that row */
      /* L2: */
      ++q[k];
    }
    L3: ;
  }


  /* --phase 2 -- find new ia and permutation to apply to (ja,a) */
  /* ----determine pointers to delimit rows in permuted (ja,a) */
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    ia[i__ + 1] = ia[i__] + q[i__];
    /* L4: */
    q[i__] = ia[i__ + 1];
  }

  /* ----determine where each (ja(j),a(j)) is stored in permuted (ja,a) */
  /* ----for each nonzero element (in reverse order) */
  ilast = 0;
  jmin = ia[1];
  jmax = ia[ *n + 1] - 1;
  j = jmax;
  i__1 = jmax;
  for (jdummy = jmin; jdummy <= i__1; ++jdummy)
  {
    i__ = r__[j];
    if (!(*dflag) || ja[j] != i__ || i__ == ilast)
    {
      goto L5;
    }

    /* ------if dflag, then put diagonal nonzero at beginning of row */
    r__[j] = ia[i__];
    ilast = i__;
    goto L6;

    /* ------put (off-diagonal) nonzero in last unused location in row */
    L5: --q[i__];
    r__[j] = q[i__];

    L6: --j;
  }


  /* --phase 3 -- permute (ja,a) to upper triangular form (wrt new ordering) */
  i__1 = jmax;
  for (j = jmin; j <= i__1; ++j)
  {
    L7: if (r__[j] == j)
    {
      goto L8;
    }
    k = r__[j];
    r__[j] = r__[k];
    r__[k] = k;
    jak = ja[k];
    ja[k] = ja[j];
    ja[j] = jak;
    ak = a[k];
    a[k] = a[j];
    a[j] = ak;
    goto L7;
    L8: ;
  }

  return 0;
} /* sro_ */

/* DECK CDRV */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::cdrv_
   (integer *n, integer *r__, integer *c__, integer *ic, integer *ia, integer
   *ja, doublereal *a, doublereal *b, doublereal *z__, integer *nsp, integer
   *isp, doublereal *rsp, integer*esp, integer *path, integer *flag__)
{
  /* Initialized data */

  static integer lratio = 2;

  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer d__, i__, j, l, q, u, ar, il, jl, iu, ju, ira, jra, ijl, max__,
    irl, iju, jrl, iru, tmp, jru, row, irac;
  static integer lmax;
  static integer umax, jlmax, jumax, jutmp;

  /* *** subroutine cdrv */
  /* *** driver for subroutines for solving sparse nonsymmetric systems of */
  /*       linear equations (compressed pointer storage) */


  /*    parameters */
  /*    class abbreviations are-- */
  /*       n - integer variable */
  /*       f - real variable */
  /*       v - supplies a value to the driver */
  /*       r - returns a result from the driver */
  /*       i - used internally by the driver */
  /*       a - array */

  /* class - parameter */
  /* ------+---------- */
  /*       - */
  /*         the nonzero entries of the coefficient matrix m are stored */
  /*    row-by-row in the array a.  to identify the individual nonzero */
  /*    entries in each row, we need to know in which column each entry */
  /*    lies.  the column indices which correspond to the nonzero entries */
  /*    of m are stored in the array ja.  i.e., if  a(k) = m(i,j),  then */
  /*    ja(k) = j.  in addition, we need to know where each row starts and */
  /*    how long it is.  the index positions in ja and a where the rows of */
  /*    m begin are stored in the array ia.  i.e., if m(i,j) is the first */
  /*    nonzero entry (stored) in the i-th row and a(k) = m(i,j),  then */
  /*    ia(i) = k.  moreover, the index in ja and a of the first location */
  /*    following the last element in the last row is stored in ia(n+1). */
  /*    thus, the number of entries in the i-th row is given by */
  /*    ia(i+1) - ia(i),  the nonzero entries of the i-th row are stored */
  /*    consecutively in */
  /*            a(ia(i)),  a(ia(i)+1),  ..., a(ia(i+1)-1), */
  /*    and the corresponding column indices are stored consecutively in */
  /*            ja(ia(i)), ja(ia(i)+1), ..., ja(ia(i+1)-1). */
  /*    for example, the 5 by 5 matrix */
  /*                ( 1. 0. 2. 0. 0.) */
  /*                ( 0. 3. 0. 0. 0.) */
  /*            m = ( 0. 4. 5. 6. 0.) */
  /*                ( 0. 0. 0. 7. 0.) */
  /*                ( 0. 0. 0. 8. 9.) */
  /*    would be stored as */
  /*               - 1  2  3  4  5  6  7  8  9 */
  /*            ---+-------------------------- */
  /*            ia - 1  3  4  7  8 10 */
  /*            ja - 1  3  2  2  3  4  4  4  5 */
  /*             a - 1. 2. 3. 4. 5. 6. 7. 8. 9.         . */

  /* nv    - n     - number of variables/equations. */
  /* fva   - a     - nonzero entries of the coefficient matrix m, stored */
  /*       -           by rows. */
  /*       -           size = number of nonzero entries in m. */
  /* nva   - ia    - pointers to delimit the rows in a. */
  /*       -           size = n+1. */
  /* nva   - ja    - column numbers corresponding to the elements of a. */
  /*       -           size = size of a. */
  /* fva   - b     - right-hand side b.  b and z can the same array. */
  /*       -           size = n. */
  /* fra   - z     - solution x.  b and z can be the same array. */
  /*       -           size = n. */

  /*         the rows and columns of the original matrix m can be */
  /*    reordered (e.g., to reduce fillin or ensure numerical stability) */
  /*    before calling the driver.  if no reordering is done, then set */
  /*    r(i) = c(i) = ic(i) = i  for i=1,...,n.  the solution z is returned */
  /*    in the original order. */
  /*         if the columns have been reordered (i.e.,  c(i).ne.i  for some */
  /*    i), then the driver will call a subroutine (nroc) which rearranges */
  /*    each row of ja and a, leaving the rows in the original order, but */
  /*    placing the elements of each row in increasing order with respect */
  /*    to the new ordering.  if  path.ne.1,  then nroc is assumed to have */
  /*    been called already. */

  /* nva   - r     - ordering of the rows of m. */
  /*       -           size = n. */
  /* nva   - c     - ordering of the columns of m. */
  /*       -           size = n. */
  /* nva   - ic    - inverse of the ordering of the columns of m.  i.e., */
  /*       -           ic(c(i)) = i  for i=1,...,n. */
  /*       -           size = n. */

  /*         the solution of the system of linear equations is divided into */
  /*    three stages -- */
  /*      nsfc -- the matrix m is processed symbolically to determine where */
  /*               fillin will occur during the numeric factorization. */
  /*      nnfc -- the matrix m is factored numerically into the product ldu */
  /*               of a unit lower triangular matrix l, a diagonal matrix */
  /*               d, and a unit upper triangular matrix u, and the system */
  /*               mx = b  is solved. */
  /*      nnsc -- the linear system  mx = b  is solved using the ldu */
  /*  or           factorization from nnfc. */
  /*      nntc -- the transposed linear system  mt x = b  is solved using */
  /*               the ldu factorization from nnf. */
  /*    for several systems whose coefficient matrices have the same */
  /*    nonzero structure, nsfc need be done only once (for the first */
  /*    system).  then nnfc is done once for each additional system.  for */
  /*    several systems with the same coefficient matrix, nsfc and nnfc */
  /*    need be done only once (for the first system).  then nnsc or nntc */
  /*    is done once for each additional right-hand side. */

  /* nv    - path  - path specification.  values and their meanings are -- */
  /*       -           1  perform nroc, nsfc, and nnfc. */
  /*       -           2  perform nnfc only  (nsfc is assumed to have been */
  /*       -               done in a manner compatible with the storage */
  /*       -               allocation used in the driver). */
  /*       -           3  perform nnsc only  (nsfc and nnfc are assumed to */
  /*       -               have been done in a manner compatible with the */
  /*       -               storage allocation used in the driver). */
  /*       -           4  perform nntc only  (nsfc and nnfc are assumed to */
  /*       -               have been done in a manner compatible with the */
  /*       -               storage allocation used in the driver). */
  /*       -           5  perform nroc and nsfc. */

  /*         various errors are detected by the driver and the individual */
  /*    subroutines. */

  /* nr    - flag  - error flag.  values and their meanings are -- */
  /*       -             0     no errors detected */
  /*       -             n+k   null row in a  --  row = k */
  /*       -            2n+k   duplicate entry in a  --  row = k */
  /*       -            3n+k   insufficient storage in nsfc  --  row = k */
  /*       -            4n+1   insufficient storage in nnfc */
  /*       -            5n+k   null pivot  --  row = k */
  /*       -            6n+k   insufficient storage in nsfc  --  row = k */
  /*       -            7n+1   insufficient storage in nnfc */
  /*       -            8n+k   zero pivot  --  row = k */
  /*       -           10n+1   insufficient storage in cdrv */
  /*       -           11n+1   illegal path specification */

  /*         working storage is needed for the factored form of the matrix */
  /*    m plus various temporary vectors.  the arrays isp and rsp should be */
  /*    equivalenced.  integer storage is allocated from the beginning of */
  /*    isp and real storage from the end of rsp. */

  /* nv    - nsp   - declared dimension of rsp.  nsp generally must */
  /*       -           be larger than  8n+2 + 2k  (where  k = (number of */
  /*       -           nonzero entries in m)). */
  /* nvira - isp   - integer working storage divided up into various arrays */
  /*       -           needed by the subroutines.  isp and rsp should be */
  /*       -           equivalenced. */
  /*       -           size = lratio*nsp. */
  /* fvira - rsp   - real working storage divided up into various arrays */
  /*       -           needed by the subroutines.  isp and rsp should be */
  /*       -           equivalenced. */
  /*       -           size = nsp. */
  /* nr    - esp   - if sufficient storage was available to perform the */
  /*       -           symbolic factorization (nsfc), then esp is set to */
  /*       -           the amount of excess storage provided (negative if */
  /*       -           insufficient storage was available to perform the */
  /*       -           numeric factorization (nnfc)). */


  /*  conversion to double precision */

  /*    to convert these routines for double precision arrays.. */
  /*    (1) use the double precision declarations in place of the real */
  /*    declarations in each subprogram, as given in comment cards. */
  /*    (2) change the data-loaded value of the integer  lratio */
  /*    in subroutine cdrv, as indicated below. */
  /*    (3) change e0 to d0 in the constants in statement number 10 */
  /*    in subroutine nnfc and the line following that. */

  /*     real  a(*), b(*), z(*), rsp(*) */

  /*  set lratio equal to the ratio between the length of floating point */
  /*  and integer array data.  e. g., lratio = 1 for (real, integer), */
  /*  lratio = 2 for (double precision, integer) */

  /* Parameter adjustments */
  --rsp;
  --isp;
  --z__;
  --b;
  --a;
  --ja;
  --ia;
  --ic;
  --c__;
  --r__;

  /* Function Body */

  if (*path < 1 || 5 <  *path)
  {
    goto L111;
  }
  /* ******initialize and divide up temporary storage  ******************* */
  il = 1;
  ijl = il + (*n + 1);
  iu = ijl +  *n;
  iju = iu + (*n + 1);
  irl = iju +  *n;
  jrl = irl +  *n;
  jl = jrl +  *n;

  /*  ******  reorder a if necessary, call nsfc if flag is set  *********** */
  if ((*path - 1)*(*path - 5) != 0)
  {
    goto L5;
  }
  max__ = lratio **nsp + 1-jl - (*n + 1) -  *n * 5;
  jlmax = max__ / 2;
  q = jl + jlmax;
  ira = q + (*n + 1);
  jra = ira +  *n;
  irac = jra +  *n;
  iru = irac +  *n;
  jru = iru +  *n;
  jutmp = jru +  *n;
  jumax = lratio **nsp + 1-jutmp;
  *esp = max__ / lratio;
  if (jlmax <= 0 || jumax <= 0)
  {
    goto L110;
  }

  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    if (c__[i__] != i__)
    {
      goto L2;
    }
    /* L1: */
  }
  goto L3;
  L2: ar =  *nsp + 1- *n;
  nroc_(n, &ic[1], &ia[1], &ja[1], &a[1], &isp[il], &rsp[ar], &isp[iu], flag__);
  if (*flag__ != 0)
  {
    goto L100;
  }

  L3: nsfc_(n, &r__[1], &ic[1], &ia[1], &ja[1], &jlmax, &isp[il], &isp[jl],
    &isp[ijl], &jumax, &isp[iu], &isp[jutmp], &isp[iju], &isp[q], &isp[ira],
    &isp[jra], &isp[irac], &isp[irl], &isp[jrl], &isp[iru], &isp[jru], flag__);
  if (*flag__ != 0)
  {
    goto L100;
  }
  /*  ******  move ju next to jl  ***************************************** */
  jlmax = isp[ijl +  *n - 1];
  ju = jl + jlmax;
  jumax = isp[iju +  *n - 1];
  if (jumax <= 0)
  {
    goto L5;
  }
  i__1 = jumax;
  for (j = 1; j <= i__1; ++j)
  {
    /* L4: */
    isp[ju + j - 1] = isp[jutmp + j - 1];
  }

  /*  ******  call remaining subroutines  ********************************* */
  L5: jlmax = isp[ijl +  *n - 1];
  ju = jl + jlmax;
  jumax = isp[iju +  *n - 1];
  l = (ju + jumax - 2+lratio) / lratio + 1;
  lmax = isp[il +  *n] - 1;
  d__ = l + lmax;
  u = d__ +  *n;
  row =  *nsp + 1- *n;
  tmp = row -  *n;
  umax = tmp - u;
  *esp = umax - (isp[iu +  *n] - 1);

  if ((*path - 1)*(*path - 2) != 0)
  {
    goto L6;
  }
  if (umax < 0)
  {
    goto L110;
  }
  nnfc_(n, &r__[1], &c__[1], &ic[1], &ia[1], &ja[1], &a[1], &z__[1], &b[1], 
    &lmax, &isp[il], &isp[jl], &isp[ijl], &rsp[l], &rsp[d__], &umax, &isp[iu],
    &isp[ju], &isp[iju], &rsp[u], &rsp[row], &rsp[tmp], &isp[irl], &isp[jrl],
    flag__);
  if (*flag__ != 0)
  {
    goto L100;
  }

  L6: if (*path - 3 != 0)
  {
    goto L7;
  }
  nnsc_(n, &r__[1], &c__[1], &isp[il], &isp[jl], &isp[ijl], &rsp[l], &rsp[d__],
    &isp[iu], &isp[ju], &isp[iju], &rsp[u], &z__[1], &b[1], &rsp[tmp]);

  L7: if (*path - 4 != 0)
  {
    goto L8;
  }
  nntc_(n, &r__[1], &c__[1], &isp[il], &isp[jl], &isp[ijl], &rsp[l], &rsp[d__],
    &isp[iu], &isp[ju], &isp[iju], &rsp[u], &z__[1], &b[1], &rsp[tmp]);
  L8: return 0;

  /* ** error.. error detected in nroc, nsfc, nnfc, or nnsc */
  L100: return 0;
  /* ** error.. insufficient storage */
  L110: *flag__ =  *n * 10+1;
  return 0;
  /* ** error.. illegal path specification */
  L111: *flag__ =  *n * 11+1;
  return 0;
} /* cdrv_ */

 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::nroc_
   (integer *n, integer *ic, integer *ia, integer *ja, doublereal *a, integer
   *jar, doublereal *ar, integer *p, integer *flag__)
{
  /* System generated locals */
  integer i__1, i__2;

  /* Local variables */
  static integer i__, j, k, jmin, jmax, newj;


  /*       ---------------------------------------------------------------- */

  /*               yale sparse matrix package - nonsymmetric codes */
  /*                    solving the system of equations mx = b */

  /*    i.   calling sequences */
  /*         the coefficient matrix can be processed by an ordering routine */
  /*    (e.g., to reduce fillin or ensure numerical stability) before using */
  /*    the remaining subroutines.  if no reordering is done, then set */
  /*    r(i) = c(i) = ic(i) = i  for i=1,...,n.  if an ordering subroutine */
  /*    is used, then nroc should be used to reorder the coefficient matrix */
  /*    the calling sequence is -- */
  /*        (       (matrix ordering)) */
  /*        (nroc   (matrix reordering)) */
  /*         nsfc   (symbolic factorization to determine where fillin will */
  /*                  occur during numeric factorization) */
  /*         nnfc   (numeric factorization into product ldu of unit lower */
  /*                  triangular matrix l, diagonal matrix d, and unit */
  /*                  upper triangular matrix u, and solution of linear */
  /*                  system) */
  /*         nnsc   (solution of linear system for additional right-hand */
  /*                  side using ldu factorization from nnfc) */
  /*    (if only one system of equations is to be solved, then the */
  /*    subroutine trk should be used.) */

  /*    ii.  storage of sparse matrices */
  /*         the nonzero entries of the coefficient matrix m are stored */
  /*    row-by-row in the array a.  to identify the individual nonzero */
  /*    entries in each row, we need to know in which column each entry */
  /*    lies.  the column indices which correspond to the nonzero entries */
  /*    of m are stored in the array ja.  i.e., if  a(k) = m(i,j),  then */
  /*    ja(k) = j.  in addition, we need to know where each row starts and */
  /*    how long it is.  the index positions in ja and a where the rows of */
  /*    m begin are stored in the array ia.  i.e., if m(i,j) is the first */
  /*    (leftmost) entry in the i-th row and  a(k) = m(i,j),  then */
  /*    ia(i) = k.  moreover, the index in ja and a of the first location */
  /*    following the last element in the last row is stored in ia(n+1). */
  /*    thus, the number of entries in the i-th row is given by */
  /*    ia(i+1) - ia(i),  the nonzero entries of the i-th row are stored */
  /*    consecutively in */
  /*            a(ia(i)),  a(ia(i)+1),  ..., a(ia(i+1)-1), */
  /*    and the corresponding column indices are stored consecutively in */
  /*            ja(ia(i)), ja(ia(i)+1), ..., ja(ia(i+1)-1). */
  /*    for example, the 5 by 5 matrix */
  /*                ( 1. 0. 2. 0. 0.) */
  /*                ( 0. 3. 0. 0. 0.) */
  /*            m = ( 0. 4. 5. 6. 0.) */
  /*                ( 0. 0. 0. 7. 0.) */
  /*                ( 0. 0. 0. 8. 9.) */
  /*    would be stored as */
  /*               - 1  2  3  4  5  6  7  8  9 */
  /*            ---+-------------------------- */
  /*            ia - 1  3  4  7  8 10 */
  /*            ja - 1  3  2  2  3  4  4  4  5 */
  /*             a - 1. 2. 3. 4. 5. 6. 7. 8. 9.         . */

  /*         the strict upper (lower) triangular portion of the matrix */
  /*    u (l) is stored in a similar fashion using the arrays  iu, ju, u */
  /*    (il, jl, l)  except that an additional array iju (ijl) is used to */
  /*    compress storage of ju (jl) by allowing some sequences of column */
  /*    (row) indices to used for more than one row (column)  (n.b., l is */
  /*    stored by columns).  iju(k) (ijl(k)) points to the starting */
  /*    location in ju (jl) of entries for the kth row (column). */
  /*    compression in ju (jl) occurs in two ways.  first, if a row */
  /*    (column) i was merged into the current row (column) k, and the */
  /*    number of elements merged in from (the tail portion of) row */
  /*    (column) i is the same as the final length of row (column) k, then */
  /*    the kth row (column) and the tail of row (column) i are identical */
  /*    and iju(k) (ijl(k)) points to the start of the tail.  second, if */
  /*    some tail portion of the (k-1)st row (column) is identical to the */
  /*    head of the kth row (column), then iju(k) (ijl(k)) points to the */
  /*    start of that tail portion.  for example, the nonzero structure of */
  /*    the strict upper triangular part of the matrix */
  /*            d 0 x x x */
  /*            0 d 0 x x */
  /*            0 0 d x 0 */
  /*            0 0 0 d x */
  /*            0 0 0 0 d */
  /*    would be represented as */
  /*                - 1 2 3 4 5 6 */
  /*            ----+------------ */
  /*             iu - 1 4 6 7 8 8 */
  /*             ju - 3 4 5 4 */
  /*            iju - 1 2 4 3           . */
  /*    the diagonal entries of l and u are assumed to be equal to one and */
  /*    are not stored.  the array d contains the reciprocals of the */
  /*    diagonal entries of the matrix d. */

  /*    iii. additional storage savings */
  /*         in nsfc, r and ic can be the same array in the calling */
  /*    sequence if no reordering of the coefficient matrix has been done. */
  /*         in nnfc, r, c, and ic can all be the same array if no */
  /*    reordering has been done.  if only the rows have been reordered, */
  /*    then c and ic can be the same array.  if the row and column */
  /*    orderings are the same, then r and c can be the same array.  z and */
  /*    row can be the same array. */
  /*         in nnsc or nntc, r and c can be the same array if no */
  /*    reordering has been done or if the row and column orderings are the */
  /*    same.  z and b can be the same array.  however, then b will be */
  /*    destroyed. */

  /*    iv.  parameters */
  /*         following is a list of parameters to the programs.  names are */
  /*    uniform among the various subroutines.  class abbreviations are -- */
  /*       n - integer variable */
  /*       f - real variable */
  /*       v - supplies a value to a subroutine */
  /*       r - returns a result from a subroutine */
  /*       i - used internally by a subroutine */
  /*       a - array */

  /* class - parameter */
  /* ------+---------- */
  /* fva   - a     - nonzero entries of the coefficient matrix m, stored */
  /*       -           by rows. */
  /*       -           size = number of nonzero entries in m. */
  /* fva   - b     - right-hand side b. */
  /*       -           size = n. */
  /* nva   - c     - ordering of the columns of m. */
  /*       -           size = n. */
  /* fvra  - d     - reciprocals of the diagonal entries of the matrix d. */
  /*       -           size = n. */
  /* nr    - flag  - error flag.  values and their meanings are -- */
  /*       -            0     no errors detected */
  /*       -            n+k   null row in a  --  row = k */
  /*       -           2n+k   duplicate entry in a  --  row = k */
  /*       -           3n+k   insufficient storage for jl  --  row = k */
  /*       -           4n+1   insufficient storage for l */
  /*       -           5n+k   null pivot  --  row = k */
  /*       -           6n+k   insufficient storage for ju  --  row = k */
  /*       -           7n+1   insufficient storage for u */
  /*       -           8n+k   zero pivot  --  row = k */
  /* nva   - ia    - pointers to delimit the rows of a. */
  /*       -           size = n+1. */
  /* nvra  - ijl   - pointers to the first element in each column in jl, */
  /*       -           used to compress storage in jl. */
  /*       -           size = n. */
  /* nvra  - iju   - pointers to the first element in each row in ju, used */
  /*       -           to compress storage in ju. */
  /*       -           size = n. */
  /* nvra  - il    - pointers to delimit the columns of l. */
  /*       -           size = n+1. */
  /* nvra  - iu    - pointers to delimit the rows of u. */
  /*       -           size = n+1. */
  /* nva   - ja    - column numbers corresponding to the elements of a. */
  /*       -           size = size of a. */
  /* nvra  - jl    - row numbers corresponding to the elements of l. */
  /*       -           size = jlmax. */
  /* nv    - jlmax - declared dimension of jl.  jlmax must be larger than */
  /*       -           the number of nonzeros in the strict lower triangle */
  /*       -           of m plus fillin minus compression. */
  /* nvra  - ju    - column numbers corresponding to the elements of u. */
  /*       -           size = jumax. */
  /* nv    - jumax - declared dimension of ju.  jumax must be larger than */
  /*       -           the number of nonzeros in the strict upper triangle */
  /*       -           of m plus fillin minus compression. */
  /* fvra  - l     - nonzero entries in the strict lower triangular portion */
  /*       -           of the matrix l, stored by columns. */
  /*       -           size = lmax. */
  /* nv    - lmax  - declared dimension of l.  lmax must be larger than */
  /*       -           the number of nonzeros in the strict lower triangle */
  /*       -           of m plus fillin  (il(n+1)-1 after nsfc). */
  /* nv    - n     - number of variables/equations. */
  /* nva   - r     - ordering of the rows of m. */
  /*       -           size = n. */
  /* fvra  - u     - nonzero entries in the strict upper triangular portion */
  /*       -           of the matrix u, stored by rows. */
  /*       -           size = umax. */
  /* nv    - umax  - declared dimension of u.  umax must be larger than */
  /*       -           the number of nonzeros in the strict upper triangle */
  /*       -           of m plus fillin  (iu(n+1)-1 after nsfc). */
  /* fra   - z     - solution x. */
  /*       -           size = n. */

  /*       ---------------------------------------------------------------- */

  /* *** subroutine nroc */
  /* *** reorders rows of a, leaving row order unchanged */


  /*       input parameters.. n, ic, ia, ja, a */
  /*       output parameters.. ja, a, flag */

  /*       parameters used internally.. */
  /* nia   - p     - at the kth step, p is a linked list of the reordered */
  /*       -           column indices of the kth row of a.  p(n+1) points */
  /*       -           to the first entry in the list. */
  /*       -           size = n+1. */
  /* nia   - jar   - at the kth step,jar contains the elements of the */
  /*       -           reordered column indices of a. */
  /*       -           size = n. */
  /* fia   - ar    - at the kth step, ar contains the elements of the */
  /*       -           reordered row of a. */
  /*       -           size = n. */

  /*     real  a(*), ar(*) */

  /*  ******  for each nonempty row  ******************************* */
  /* Parameter adjustments */
  --p;
  --ar;
  --jar;
  --a;
  --ja;
  --ia;
  --ic;

  /* Function Body */
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    jmin = ia[k];
    jmax = ia[k + 1] - 1;
    if (jmin > jmax)
    {
      goto L5;
    }
    p[ *n + 1] =  *n + 1;
    /*  ******  insert each element in the list  ********************* */
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      newj = ic[ja[j]];
      i__ =  *n + 1;
      L1: if (p[i__] >= newj)
      {
        goto L2;
      }
      i__ = p[i__];
      goto L1;
      L2: if (p[i__] == newj)
      {
        goto L102;
      }
      p[newj] = p[i__];
      p[i__] = newj;
      jar[newj] = ja[j];
      ar[newj] = a[j];
      /* L3: */
    }
    /*  ******  replace old row in ja and a  ************************* */
    i__ =  *n + 1;
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      i__ = p[i__];
      ja[j] = jar[i__];
      /* L4: */
      a[j] = ar[i__];
    }
    L5: ;
  }
  *flag__ = 0;
  return 0;

  /* ** error.. duplicate entry in a */
  L102: *flag__ =  *n + k;
  return 0;
} /* nroc_ */

 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::nsfc_
   (integer *n, integer *r__, integer *ic, integer *ia, integer *ja, integer
   *jlmax, integer *il, integer *jl, integer *ijl, integer *jumax, integer *iu,
   integer *ju, integer *iju, integer *q, integer *ira, integer *jra, integer
   *irac, integer *irl, integer *jrl, integer *iru, integer *jru, integer
   *flag__)
{
  /* System generated locals */
  integer i__1, i__2, i__3;

  /* Local variables */
  static integer i__, j, k, m, i1, rk, qm, vj, np1, iak, luk, cend, irai, rend,
    jmin, jmax, long__, irll, jtmp, irul, jaiak, jlmin, lasti, jumin, jlptr,
    juptr, jairai, lastid;

  /* *** subroutine nsfc */
  /* *** symbolic ldu-factorization of nonsymmetric sparse matrix */
  /*      (compressed pointer storage) */


  /*       input variables.. n, r, ic, ia, ja, jlmax, jumax. */
  /*       output variables.. il, jl, ijl, iu, ju, iju, flag. */

  /*       parameters used internally.. */
  /* nia   - q     - suppose  m*  is the result of reordering  m.  if */
  /*       -           processing of the ith row of  m*  (hence the ith */
  /*       -           row of  u) is being done,  q(j)  is initially */
  /*       -           nonzero if  m*(i,j) is nonzero (j.ge.i).  since */
  /*       -           values need not be stored, each entry points to the */
  /*       -           next nonzero and  q(n+1)  points to the first.  n+1 */
  /*       -           indicates the end of the list.  for example, if n=9 */
  /*       -           and the 5th row of  m*  is */
  /*       -              0 x x 0 x 0 0 x 0 */
  /*       -           then  q  will initially be */
  /*       -              a a a a 8 a a 10 5           (a - arbitrary). */
  /*       -           as the algorithm proceeds, other elements of  q */
  /*       -           are inserted in the list because of fillin. */
  /*       -           q  is used in an analogous manner to compute the */
  /*       -           ith column of  l. */
  /*       -           size = n+1. */
  /* nia   - ira,  - vectors used to find the columns of  m.  at the kth */
  /* nia   - jra,      step of the factorization,  irac(k)  points to the */
  /* nia   - irac      head of a linked list in  jra  of row indices i */
  /*       -           such that i .ge. k and  m(i,k)  is nonzero.  zero */
  /*       -           indicates the end of the list.  ira(i)  (i.ge.k) */
  /*       -           points to the smallest j such that j .ge. k and */
  /*       -           m(i,j)  is nonzero. */
  /*       -           size of each = n. */
  /* nia   - irl,  - vectors used to find the rows of  l.  at the kth step */
  /* nia   - jrl       of the factorization,  jrl(k)  points to the head */
  /*       -           of a linked list in  jrl  of column indices j */
  /*       -           such j .lt. k and  l(k,j)  is nonzero.  zero */
  /*       -           indicates the end of the list.  irl(j)  (j.lt.k) */
  /*       -           points to the smallest i such that i .ge. k and */
  /*       -           l(i,j)  is nonzero. */
  /*       -           size of each = n. */
  /* nia   - iru,  - vectors used in a manner analogous to  irl and jrl */
  /* nia   - jru       to find the columns of  u. */
  /*       -           size of each = n. */

  /*  internal variables.. */
  /*    jlptr - points to the last position used in  jl. */
  /*    juptr - points to the last position used in  ju. */
  /*    jmin,jmax - are the indices in  a or u  of the first and last */
  /*                elements to be examined in a given row. */
  /*                for example,  jmin=ia(k), jmax=ia(k+1)-1. */


  /*  ******  initialize pointers  **************************************** */
  /* Parameter adjustments */
  --jru;
  --iru;
  --jrl;
  --irl;
  --irac;
  --jra;
  --ira;
  --q;
  --iju;
  --ju;
  --iu;
  --ijl;
  --jl;
  --il;
  --ja;
  --ia;
  --ic;
  --r__;

  /* Function Body */
  np1 =  *n + 1;
  jlmin = 1;
  jlptr = 0;
  il[1] = 1;
  jumin = 1;
  juptr = 0;
  iu[1] = 1;
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    irac[k] = 0;
    jra[k] = 0;
    jrl[k] = 0;
    /* L1: */
    jru[k] = 0;
  }
  /*  ******  initialize column pointers for a  *************************** */
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    rk = r__[k];
    iak = ia[rk];
    if (iak >= ia[rk + 1])
    {
      goto L101;
    }
    jaiak = ic[ja[iak]];
    if (jaiak > k)
    {
      goto L105;
    }
    jra[k] = irac[jaiak];
    irac[jaiak] = k;
    /* L2: */
    ira[k] = iak;
  }

  /*  ******  for each column of l and row of u  ************************** */
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {

    /*  ******  initialize q for computing kth column of l  ***************** */
    q[np1] = np1;
    luk =  - 1;
    /*  ******  by filling in kth column of a  ****************************** */
    vj = irac[k];
    if (vj == 0)
    {
      goto L5;
    }
    L3: qm = np1;
    L4: m = qm;
    qm = q[m];
    if (qm < vj)
    {
      goto L4;
    }
    if (qm == vj)
    {
      goto L102;
    }
    ++luk;
    q[m] = vj;
    q[vj] = qm;
    vj = jra[vj];
    if (vj != 0)
    {
      goto L3;
    }
    /*  ******  link through jru  ******************************************* */
    L5: lastid = 0;
    lasti = 0;
    ijl[k] = jlptr;
    i__ = k;
    L6: i__ = jru[i__];
    if (i__ == 0)
    {
      goto L10;
    }
    qm = np1;
    jmin = irl[i__];
    jmax = ijl[i__] + il[i__ + 1] - il[i__] - 1;
    long__ = jmax - jmin;
    if (long__ < 0)
    {
      goto L6;
    }
    jtmp = jl[jmin];
    if (jtmp != k)
    {
      ++long__;
    }
    if (jtmp == k)
    {
      r__[i__] =  - r__[i__];
    }
    if (lastid >= long__)
    {
      goto L7;
    }
    lasti = i__;
    lastid = long__;
    /*  ******  and merge the corresponding columns into the kth column  **** */
    L7: i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      vj = jl[j];
      L8: m = qm;
      qm = q[m];
      if (qm < vj)
      {
        goto L8;
      }
      if (qm == vj)
      {
        goto L9;
      }
      ++luk;
      q[m] = vj;
      q[vj] = qm;
      qm = vj;
      L9: ;
    }
    goto L6;
    /*  ******  lasti is the longest column merged into the kth  ************ */
    /*  ******  see if it equals the entire kth column  ********************* */
    L10: qm = q[np1];
    if (qm != k)
    {
      goto L105;
    }
    if (luk == 0)
    {
      goto L17;
    }
    if (lastid != luk)
    {
      goto L11;
    }
    /*  ******  if so, jl can be compressed  ******************************** */
    irll = irl[lasti];
    ijl[k] = irll + 1;
    if (jl[irll] != k)
    {
      --ijl[k];
    }
    goto L17;
    /*  ******  if not, see if kth column can overlap the previous one  ***** */
    L11: if (jlmin > jlptr)
    {
      goto L15;
    }
    qm = q[qm];
    i__2 = jlptr;
    for (j = jlmin; j <= i__2; ++j)
    {
      if ((i__3 = jl[j] - qm) < 0)
      {
        goto L12;
      }
      else if (i__3 == 0)
      {
        goto L13;
      }
      else
      {
        goto L15;
      }
      L12: ;
    }
    goto L15;
    L13: ijl[k] = j;
    i__2 = jlptr;
    for (i__ = j; i__ <= i__2; ++i__)
    {
      if (jl[i__] != qm)
      {
        goto L15;
      }
      qm = q[qm];
      if (qm >  *n)
      {
        goto L17;
      }
      /* L14: */
    }
    jlptr = j - 1;
    /*  ******  move column indices from q to jl, update vectors  *********** */
    L15: jlmin = jlptr + 1;
    ijl[k] = jlmin;
    if (luk == 0)
    {
      goto L17;
    }
    jlptr += luk;
    if (jlptr >  *jlmax)
    {
      goto L103;
    }
    qm = q[np1];
    i__2 = jlptr;
    for (j = jlmin; j <= i__2; ++j)
    {
      qm = q[qm];
      /* L16: */
      jl[j] = qm;
    }
    L17: irl[k] = ijl[k];
    il[k + 1] = il[k] + luk;

    /*  ******  initialize q for computing kth row of u  ******************** */
    q[np1] = np1;
    luk =  - 1;
    /*  ******  by filling in kth row of reordered a  *********************** */
    rk = r__[k];
    jmin = ira[k];
    jmax = ia[rk + 1] - 1;
    if (jmin > jmax)
    {
      goto L20;
    }
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      vj = ic[ja[j]];
      qm = np1;
      L18: m = qm;
      qm = q[m];
      if (qm < vj)
      {
        goto L18;
      }
      if (qm == vj)
      {
        goto L102;
      }
      ++luk;
      q[m] = vj;
      q[vj] = qm;
      /* L19: */
    }
    /*  ******  link through jrl,  ****************************************** */
    L20: lastid = 0;
    lasti = 0;
    iju[k] = juptr;
    i__ = k;
    i1 = jrl[k];
    L21: i__ = i1;
    if (i__ == 0)
    {
      goto L26;
    }
    i1 = jrl[i__];
    qm = np1;
    jmin = iru[i__];
    jmax = iju[i__] + iu[i__ + 1] - iu[i__] - 1;
    long__ = jmax - jmin;
    if (long__ < 0)
    {
      goto L21;
    }
    jtmp = ju[jmin];
    if (jtmp == k)
    {
      goto L22;
    }
    /*  ******  update irl and jrl, ***************************************** */
    ++long__;
    cend = ijl[i__] + il[i__ + 1] - il[i__];
    ++irl[i__];
    if (irl[i__] >= cend)
    {
      goto L22;
    }
    j = jl[irl[i__]];
    jrl[i__] = jrl[j];
    jrl[j] = i__;
    L22: if (lastid >= long__)
    {
      goto L23;
    }
    lasti = i__;
    lastid = long__;
    /*  ******  and merge the corresponding rows into the kth row  ********** */
    L23: i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      vj = ju[j];
      L24: m = qm;
      qm = q[m];
      if (qm < vj)
      {
        goto L24;
      }
      if (qm == vj)
      {
        goto L25;
      }
      ++luk;
      q[m] = vj;
      q[vj] = qm;
      qm = vj;
      L25: ;
    }
    goto L21;
    /*  ******  update jrl(k) and irl(k)  *********************************** */
    L26: if (il[k + 1] <= il[k])
    {
      goto L27;
    }
    j = jl[irl[k]];
    jrl[k] = jrl[j];
    jrl[j] = k;
    /*  ******  lasti is the longest row merged into the kth  *************** */
    /*  ******  see if it equals the entire kth row  ************************ */
    L27: qm = q[np1];
    if (qm != k)
    {
      goto L105;
    }
    if (luk == 0)
    {
      goto L34;
    }
    if (lastid != luk)
    {
      goto L28;
    }
    /*  ******  if so, ju can be compressed  ******************************** */
    irul = iru[lasti];
    iju[k] = irul + 1;
    if (ju[irul] != k)
    {
      --iju[k];
    }
    goto L34;
    /*  ******  if not, see if kth row can overlap the previous one  ******** */
    L28: if (jumin > juptr)
    {
      goto L32;
    }
    qm = q[qm];
    i__2 = juptr;
    for (j = jumin; j <= i__2; ++j)
    {
      if ((i__3 = ju[j] - qm) < 0)
      {
        goto L29;
      }
      else if (i__3 == 0)
      {
        goto L30;
      }
      else
      {
        goto L32;
      }
      L29: ;
    }
    goto L32;
    L30: iju[k] = j;
    i__2 = juptr;
    for (i__ = j; i__ <= i__2; ++i__)
    {
      if (ju[i__] != qm)
      {
        goto L32;
      }
      qm = q[qm];
      if (qm >  *n)
      {
        goto L34;
      }
      /* L31: */
    }
    juptr = j - 1;
    /*  ******  move row indices from q to ju, update vectors  ************** */
    L32: jumin = juptr + 1;
    iju[k] = jumin;
    if (luk == 0)
    {
      goto L34;
    }
    juptr += luk;
    if (juptr >  *jumax)
    {
      goto L106;
    }
    qm = q[np1];
    i__2 = juptr;
    for (j = jumin; j <= i__2; ++j)
    {
      qm = q[qm];
      /* L33: */
      ju[j] = qm;
    }
    L34: iru[k] = iju[k];
    iu[k + 1] = iu[k] + luk;

    /*  ******  update iru, jru  ******************************************** */
    i__ = k;
    L35: i1 = jru[i__];
    if (r__[i__] < 0)
    {
      goto L36;
    }
    rend = iju[i__] + iu[i__ + 1] - iu[i__];
    if (iru[i__] >= rend)
    {
      goto L37;
    }
    j = ju[iru[i__]];
    jru[i__] = jru[j];
    jru[j] = i__;
    goto L37;
    L36: r__[i__] =  - r__[i__];
    L37: i__ = i1;
    if (i__ == 0)
    {
      goto L38;
    }
    ++iru[i__];
    goto L35;

    /*  ******  update ira, jra, irac  ************************************** */
    L38: i__ = irac[k];
    if (i__ == 0)
    {
      goto L41;
    }
    L39: i1 = jra[i__];
    ++ira[i__];
    if (ira[i__] >= ia[r__[i__] + 1])
    {
      goto L40;
    }
    irai = ira[i__];
    jairai = ic[ja[irai]];
    if (jairai > i__)
    {
      goto L40;
    }
    jra[i__] = irac[jairai];
    irac[jairai] = i__;
    L40: i__ = i1;
    if (i__ != 0)
    {
      goto L39;
    }
    L41: ;
  }

  ijl[ *n] = jlptr;
  iju[ *n] = juptr;
  *flag__ = 0;
  return 0;

  /* ** error.. null row in a */
  L101: *flag__ =  *n + rk;
  return 0;
  /* ** error.. duplicate entry in a */
  L102: *flag__ = (*n << 1) + rk;
  return 0;
  /* ** error.. insufficient storage for jl */
  L103: *flag__ =  *n * 3+k;
  return 0;
  /* ** error.. null pivot */
  L105: *flag__ =  *n * 5+k;
  return 0;
  /* ** error.. insufficient storage for ju */
  L106: *flag__ =  *n * 6+k;
  return 0;
} /* nsfc_ */

 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::nnfc_
   (integer *n, integer *r__, integer *c__, integer *ic, integer *ia, integer
   *ja, doublereal *a, doublereal *z__, doublereal *b, integer *lmax, integer
   *il, integer *jl, integer *ijl, doublereal *l, doublereal *d__, integer
   *umax, integer *iu, integer *ju, integer *iju, doublereal *u, doublereal
   *row, doublereal *tmp, integer *irl, integer *jrl, integer *flag__)
{
  /* System generated locals */
  integer i__1, i__2;

  /* Local variables */
  static integer i__, j, k, i1, i2;
  static doublereal dk;
  static integer rk, mu;
  static doublereal lki, sum;
  static integer ijlb, jmin, jmax;

  /* *** subroutine nnfc */
  /* *** numerical ldu-factorization of sparse nonsymmetric matrix and */
  /*      solution of system of linear equations (compressed pointer */
  /*      storage) */


  /*       input variables..  n, r, c, ic, ia, ja, a, b, */
  /*                          il, jl, ijl, lmax, iu, ju, iju, umax */
  /*       output variables.. z, l, d, u, flag */

  /*       parameters used internally.. */
  /* nia   - irl,  - vectors used to find the rows of  l.  at the kth step */
  /* nia   - jrl       of the factorization,  jrl(k)  points to the head */
  /*       -           of a linked list in  jrl  of column indices j */
  /*       -           such j .lt. k and  l(k,j)  is nonzero.  zero */
  /*       -           indicates the end of the list.  irl(j)  (j.lt.k) */
  /*       -           points to the smallest i such that i .ge. k and */
  /*       -           l(i,j)  is nonzero. */
  /*       -           size of each = n. */
  /* fia   - row   - holds intermediate values in calculation of  u and l. */
  /*       -           size = n. */
  /* fia   - tmp   - holds new right-hand side  b*  for solution of the */
  /*       -           equation ux = b*. */
  /*       -           size = n. */

  /*  internal variables.. */
  /*    jmin, jmax - indices of the first and last positions in a row to */
  /*      be examined. */
  /*    sum - used in calculating  tmp. */

  /*     real  a(*), l(*), d(*), u(*), z(*), b(*), row(*) */
  /*     real tmp(*), lki, sum, dk */

  /*  ******  initialize pointers and test storage  *********************** */
  /* Parameter adjustments */
  --jrl;
  --irl;
  --tmp;
  --row;
  --u;
  --iju;
  --ju;
  --iu;
  --d__;
  --l;
  --ijl;
  --jl;
  --il;
  --b;
  --z__;
  --a;
  --ja;
  --ia;
  --ic;
  --c__;
  --r__;

  /* Function Body */
  if (il[ *n + 1] - 1 >  *lmax)
  {
    goto L104;
  }
  if (iu[ *n + 1] - 1 >  *umax)
  {
    goto L107;
  }
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    irl[k] = il[k];
    jrl[k] = 0;
    /* L1: */
  }

  /*  ******  for each row  *********************************************** */
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    /*  ******  reverse jrl and zero row where kth row of l will fill in  *** */
    row[k] = 0.;
    i1 = 0;
    if (jrl[k] == 0)
    {
      goto L3;
    }
    i__ = jrl[k];
    L2: i2 = jrl[i__];
    jrl[i__] = i1;
    i1 = i__;
    row[i__] = 0.;
    i__ = i2;
    if (i__ != 0)
    {
      goto L2;
    }
    /*  ******  set row to zero where u will fill in  *********************** */
    L3: jmin = iju[k];
    jmax = jmin + iu[k + 1] - iu[k] - 1;
    if (jmin > jmax)
    {
      goto L5;
    }
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      /* L4: */
      row[ju[j]] = 0.;
    }
    /*  ******  place kth row of a in row  ********************************** */
    L5: rk = r__[k];
    jmin = ia[rk];
    jmax = ia[rk + 1] - 1;
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      row[ic[ja[j]]] = a[j];
      /* L6: */
    }
    /*  ******  initialize sum, and link through jrl  *********************** */
    sum = b[rk];
    i__ = i1;
    if (i__ == 0)
    {
      goto L10;
    }
    /*  ******  assign the kth row of l and adjust row, sum  **************** */
    L7: lki =  - row[i__];
    /*  ******  if l is not required, then comment out the following line  ** */
    l[irl[i__]] =  - lki;
    sum += lki * tmp[i__];
    jmin = iu[i__];
    jmax = iu[i__ + 1] - 1;
    if (jmin > jmax)
    {
      goto L9;
    }
    mu = iju[i__] - jmin;
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      /* L8: */
      row[ju[mu + j]] += lki * u[j];
    }
    L9: i__ = jrl[i__];
    if (i__ != 0)
    {
      goto L7;
    }

    /*  ******  assign kth row of u and diagonal d, set tmp(k)  ************* */
    L10: if (row[k] == 0.)
    {
      goto L108;
    }
    dk = 1. / row[k];
    d__[k] = dk;
    tmp[k] = sum * dk;
    if (k ==  *n)
    {
      goto L19;
    }
    jmin = iu[k];
    jmax = iu[k + 1] - 1;
    if (jmin > jmax)
    {
      goto L12;
    }
    mu = iju[k] - jmin;
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      /* L11: */
      u[j] = row[ju[mu + j]] *dk;
    }
    L12: 

    /*  ******  update irl and jrl, keeping jrl in decreasing order  ******** */
    i__ = i1;
    if (i__ == 0)
    {
      goto L18;
    }
    L14: ++irl[i__];
    i1 = jrl[i__];
    if (irl[i__] >= il[i__ + 1])
    {
      goto L17;
    }
    ijlb = irl[i__] - il[i__] + ijl[i__];
    j = jl[ijlb];
    L15: if (i__ > jrl[j])
    {
      goto L16;
    }
    j = jrl[j];
    goto L15;
    L16: jrl[i__] = jrl[j];
    jrl[j] = i__;
    L17: i__ = i1;
    if (i__ != 0)
    {
      goto L14;
    }
    L18: if (irl[k] >= il[k + 1])
    {
      goto L19;
    }
    j = jl[ijl[k]];
    jrl[k] = jrl[j];
    jrl[j] = k;
    L19: ;
  }

  /*  ******  solve  ux = tmp  by back substitution  ********************** */
  k =  *n;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    sum = tmp[k];
    jmin = iu[k];
    jmax = iu[k + 1] - 1;
    if (jmin > jmax)
    {
      goto L21;
    }
    mu = iju[k] - jmin;
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      /* L20: */
      sum -= u[j] *tmp[ju[mu + j]];
    }
    L21: tmp[k] = sum;
    z__[c__[k]] = sum;
    /* L22: */
    --k;
  }
  *flag__ = 0;
  return 0;

  /* ** error.. insufficient storage for l */
  L104: *flag__ = (*n << 2) + 1;
  return 0;
  /* ** error.. insufficient storage for u */
  L107: *flag__ =  *n * 7+1;
  return 0;
  /* ** error.. zero pivot */
  L108: *flag__ = (*n << 3) + k;
  return 0;
} /* nnfc_ */

 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::nnsc_
   (integer *n, integer *r__, integer *c__, integer *il, integer *jl, integer
   *ijl, doublereal *l, doublereal *d__, integer *iu, integer *ju, integer *iju,
   doublereal *u, doublereal *z__, doublereal *b, doublereal *tmp)
{
  /* System generated locals */
  integer i__1, i__2;

  /* Local variables */
  static integer i__, j, k, ml, mu;
  static doublereal sum;
  static integer jmin, jmax;
  static doublereal tmpk;

  /* *** subroutine nnsc */
  /* *** numerical solution of sparse nonsymmetric system of linear */
  /*      equations given ldu-factorization (compressed pointer storage) */


  /*       input variables..  n, r, c, il, jl, ijl, l, d, iu, ju, iju, u, b */
  /*       output variables.. z */

  /*       parameters used internally.. */
  /* fia   - tmp   - temporary vector which gets result of solving  ly = b. */
  /*       -           size = n. */

  /*  internal variables.. */
  /*    jmin, jmax - indices of the first and last positions in a row of */
  /*      u or l  to be used. */

  /*     real l(*), d(*), u(*), b(*), z(*), tmp(*), tmpk, sum */

  /*  ******  set tmp to reordered b  ************************************* */
  /* Parameter adjustments */
  --tmp;
  --b;
  --z__;
  --u;
  --iju;
  --ju;
  --iu;
  --d__;
  --l;
  --ijl;
  --jl;
  --il;
  --c__;
  --r__;

  /* Function Body */
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    /* L1: */
    tmp[k] = b[r__[k]];
  }
  /*  ******  solve  ly = b  by forward substitution  ********************* */
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    jmin = il[k];
    jmax = il[k + 1] - 1;
    tmpk =  - d__[k] *tmp[k];
    tmp[k] =  - tmpk;
    if (jmin > jmax)
    {
      goto L3;
    }
    ml = ijl[k] - jmin;
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      /* L2: */
      tmp[jl[ml + j]] += tmpk * l[j];
    }
    L3: ;
  }
  /*  ******  solve  ux = y  by back substitution  ************************ */
  k =  *n;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    sum =  - tmp[k];
    jmin = iu[k];
    jmax = iu[k + 1] - 1;
    if (jmin > jmax)
    {
      goto L5;
    }
    mu = iju[k] - jmin;
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      /* L4: */
      sum += u[j] *tmp[ju[mu + j]];
    }
    L5: tmp[k] =  - sum;
    z__[c__[k]] =  - sum;
    --k;
    /* L6: */
  }
  return 0;
} /* nnsc_ */

 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::nntc_
   (integer *n, integer *r__, integer *c__, integer *il, integer *jl, integer
   *ijl, doublereal *l, doublereal *d__, integer *iu, integer *ju, integer *iju,
   doublereal *u, doublereal *z__, doublereal *b, doublereal *tmp)
{
  /* System generated locals */
  integer i__1, i__2;

  /* Local variables */
  static integer i__, j, k, ml, mu;
  static doublereal sum;
  static integer jmin, jmax;
  static doublereal tmpk;

  /* *** subroutine nntc */
  /* *** numeric solution of the transpose of a sparse nonsymmetric system */
  /*      of linear equations given lu-factorization (compressed pointer */
  /*      storage) */


  /*       input variables..  n, r, c, il, jl, ijl, l, d, iu, ju, iju, u, b */
  /*       output variables.. z */

  /*       parameters used internally.. */
  /* fia   - tmp   - temporary vector which gets result of solving ut y = b */
  /*       -           size = n. */

  /*  internal variables.. */
  /*    jmin, jmax - indices of the first and last positions in a row of */
  /*      u or l  to be used. */

  /*     real l(*), d(*), u(*), b(*), z(*), tmp(*), tmpk,sum */

  /*  ******  set tmp to reordered b  ************************************* */
  /* Parameter adjustments */
  --tmp;
  --b;
  --z__;
  --u;
  --iju;
  --ju;
  --iu;
  --d__;
  --l;
  --ijl;
  --jl;
  --il;
  --c__;
  --r__;

  /* Function Body */
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    /* L1: */
    tmp[k] = b[c__[k]];
  }
  /*  ******  solve  ut y = b  by forward substitution  ******************* */
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    jmin = iu[k];
    jmax = iu[k + 1] - 1;
    tmpk =  - tmp[k];
    if (jmin > jmax)
    {
      goto L3;
    }
    mu = iju[k] - jmin;
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      /* L2: */
      tmp[ju[mu + j]] += tmpk * u[j];
    }
    L3: ;
  }
  /*  ******  solve  lt x = y  by back substitution  ********************** */
  k =  *n;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    sum =  - tmp[k];
    jmin = il[k];
    jmax = il[k + 1] - 1;
    if (jmin > jmax)
    {
      goto L5;
    }
    ml = ijl[k] - jmin;
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      /* L4: */
      sum += l[j] *tmp[jl[ml + j]];
    }
    L5: tmp[k] =  - sum * d__[k];
    z__[r__[k]] = tmp[k];
    --k;
    /* L6: */
  }
  return 0;
} /* nntc_ */

/* DECK DSTODA */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dstoda_
   (integer *neq, doublereal *y, doublereal *yh, integer *nyh, doublereal *yh1,
   doublereal *ewt, doublereal *savf, doublereal *acor, doublereal *wm, integer
   *iwm, S_fp f, U_fp jac, S_fp pjac, S_fp slvs)
{
  /* Initialized data */

  static doublereal sm1[12] = 
  {
    .5, .575, .55, .45, .35, .25, .2, .15, .1, .075, .05, .025
  };

  /* System generated locals */
  integer yh_dim1, yh_offset, i__1, i__2;
  doublereal d__1, d__2, d__3;

  /* Local variables */
  static integer i__, j, m;
  static doublereal r__;
  static integer i1, jb;
  static doublereal rh, rm, dm1, dm2;
  static integer lm1, lm2;
  static doublereal rh1, rh2, del, ddn;
  static integer ncf;
  static doublereal pdh, dsm, dup, exm1, exm2;
  static integer nqm1, nqm2;
  static doublereal dcon, delp;
  static integer lm1p1, lm2p1;
  static doublereal exdn, rhdn;
  static integer iret;
  static doublereal told, rhsm;
  static integer newq;
  static doublereal exsm, rhup, rate, exup, rh1it, alpha;
  static integer iredo;
  static doublereal pnorm;

  /* Parameter adjustments */
  --neq;
  --y;
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --yh1;
  --ewt;
  --savf;
  --acor;
  --wm;
  --iwm;

  /* Function Body */
  /* ----------------------------------------------------------------------- */
  /* DSTODA performs one step of the integration of an initial value */
  /* problem for a system of ordinary differential equations. */
  /* Note: DSTODA is independent of the value of the iteration method */
  /* indicator MITER, when this is .ne. 0, and hence is independent */
  /* of the type of chord method used, or the Jacobian structure. */
  /* Communication with DSTODA is done with the following variables: */

  /* Y      = an array of length .ge. N used as the Y argument in */
  /*          all calls to F and JAC. */
  /* NEQ    = integer array containing problem size in NEQ(1), and */
  /*          passed as the NEQ argument in all calls to F and JAC. */
  /* YH     = an NYH by LMAX array containing the dependent variables */
  /*          and their approximate scaled derivatives, where */
  /*          LMAX = MAXORD + 1.  YH(i,j+1) contains the approximate */
  /*          j-th derivative of y(i), scaled by H**j/factorial(j) */
  /*          (j = 0,1,...,NQ).  On entry for the first step, the first */
  /*          two columns of YH must be set from the initial values. */
  /* NYH    = a constant integer .ge. N, the first dimension of YH. */
  /* YH1    = a one-dimensional array occupying the same space as YH. */
  /* EWT    = an array of length N containing multiplicative weights */
  /*          for local error measurements.  Local errors in y(i) are */
  /*          compared to 1.0/EWT(i) in various error tests. */
  /* SAVF   = an array of working storage, of length N. */
  /* ACOR   = a work array of length N, used for the accumulated */
  /*          corrections.  On a successful return, ACOR(i) contains */
  /*          the estimated one-step local error in y(i). */
  /* WM,IWM = real and integer work arrays associated with matrix */
  /*          operations in chord iteration (MITER .ne. 0). */
  /* PJAC   = name of routine to evaluate and preprocess Jacobian matrix */
  /*          and P = I - H*EL0*Jac, if a chord method is being used. */
  /*          It also returns an estimate of norm(Jac) in PDNORM. */
  /* SLVS   = name of routine to solve linear system in chord iteration. */
  /* CCMAX  = maximum relative change in H*EL0 before PJAC is called. */
  /* H      = the step size to be attempted on the next step. */
  /*          H is altered by the error control algorithm during the */
  /*          problem.  H can be either positive or negative, but its */
  /*          sign must remain constant throughout the problem. */
  /* HMIN   = the minimum absolute value of the step size H to be used. */
  /* HMXI   = inverse of the maximum absolute value of H to be used. */
  /*          HMXI = 0.0 is allowed and corresponds to an infinite HMAX. */
  /*          HMIN and HMXI may be changed at any time, but will not */
  /*          take effect until the next change of H is considered. */
  /* TN     = the independent variable. TN is updated on each step taken. */
  /* JSTART = an integer used for input only, with the following */
  /*          values and meanings: */
  /*               0  perform the first step. */
  /*           .gt.0  take a new step continuing from the last. */
  /*              -1  take the next step with a new value of H, */
  /*                    N, METH, MITER, and/or matrix parameters. */
  /*              -2  take the next step with a new value of H, */
  /*                    but with other inputs unchanged. */
  /*          On return, JSTART is set to 1 to facilitate continuation. */
  /* KFLAG  = a completion code with the following meanings: */
  /*               0  the step was succesful. */
  /*              -1  the requested error could not be achieved. */
  /*              -2  corrector convergence could not be achieved. */
  /*              -3  fatal error in PJAC or SLVS. */
  /*          A return with KFLAG = -1 or -2 means either */
  /*          ABS(H) = HMIN or 10 consecutive failures occurred. */
  /*          On a return with KFLAG negative, the values of TN and */
  /*          the YH array are as of the beginning of the last */
  /*          step, and H is the last step size attempted. */
  /* MAXORD = the maximum order of integration method to be allowed. */
  /* MAXCOR = the maximum number of corrector iterations allowed. */
  /* MSBP   = maximum number of steps between PJAC calls (MITER .gt. 0). */
  /* MXNCF  = maximum number of convergence failures allowed. */
  /* METH   = current method. */
  /*          METH = 1 means Adams method (nonstiff) */
  /*          METH = 2 means BDF method (stiff) */
  /*          METH may be reset by DSTODA. */
  /* MITER  = corrector iteration method. */
  /*          MITER = 0 means functional iteration. */
  /*          MITER = JT .gt. 0 means a chord iteration corresponding */
  /*          to Jacobian type JT.  (The DLSODA/DLSODAR argument JT is */
  /*          communicated here as JTYP, but is not used in DSTODA */
  /*          except to load MITER following a method switch.) */
  /*          MITER may be reset by DSTODA. */
  /* N      = the number of first-order differential equations. */
  /* ----------------------------------------------------------------------- */
  dls001_3.kflag = 0;
  told = dls001_3.tn;
  ncf = 0;
  dls001_3.ierpj = 0;
  dls001_3.iersl = 0;
  dls001_3.jcur = 0;
  dls001_3.icf = 0;
  delp = 0.;
  if (dls001_3.jstart > 0)
  {
    goto L200;
  }
  if (dls001_3.jstart ==  - 1)
  {
    goto L100;
  }
  if (dls001_3.jstart ==  - 2)
  {
    goto L160;
  }
  /* ----------------------------------------------------------------------- */
  /* On the first call, the order is set to 1, and other variables are */
  /* initialized.  RMAX is the maximum ratio by which H can be increased */
  /* in a single step.  It is initially 1.E4 to compensate for the small */
  /* initial H, but then is normally equal to 10.  If a failure */
  /* occurs (in corrector convergence or error test), RMAX is set at 2 */
  /* for the next increase. */
  /* DCFODE is called to get the needed coefficients for both methods. */
  /* ----------------------------------------------------------------------- */
  dls001_3.lmax = dls001_3.maxord + 1;
  dls001_3.nq = 1;
  dls001_3.l = 2;
  dls001_3.ialth = 2;
  dls001_3.rmax = 1e4;
  dls001_3.rc = 0.;
  dls001_3.el0 = 1.;
  dls001_3.crate = .7;
  dls001_3.hold = dls001_3.h__;
  dls001_3.nslp = 0;
  dls001_3.ipup = dls001_3.miter;
  iret = 3;
  /* Initialize switching parameters.  METH = 1 is assumed initially. ----- */
  dlsa01_1.icount = 20;
  dlsa01_1.irflag = 0;
  dlsa01_1.pdest = 0.;
  dlsa01_1.pdlast = 0.;
  dlsa01_1.ratio = 5.;
  dcfode_(&c__2, dls001_3.elco, dls001_3.tesco);
  for (i__ = 1; i__ <= 5; ++i__)
  {
    /* L10: */
    dlsa01_1.cm2[i__ - 1] = dls001_3.tesco[i__ *3-2] *dls001_3.elco[i__ + 1+i__
      * 13-14];
  }
  dcfode_(&c__1, dls001_3.elco, dls001_3.tesco);
  for (i__ = 1; i__ <= 12; ++i__)
  {
    /* L20: */
    dlsa01_1.cm1[i__ - 1] = dls001_3.tesco[i__ *3-2] *dls001_3.elco[i__ + 1+i__
      * 13-14];
  }
  goto L150;
  /* ----------------------------------------------------------------------- */
  /* The following block handles preliminaries needed when JSTART = -1. */
  /* IPUP is set to MITER to force a matrix update. */
  /* If an order increase is about to be considered (IALTH = 1), */
  /* IALTH is reset to 2 to postpone consideration one more step. */
  /* If the caller has changed METH, DCFODE is called to reset */
  /* the coefficients of the method. */
  /* If H is to be changed, YH must be rescaled. */
  /* If H or METH is being changed, IALTH is reset to L = NQ + 1 */
  /* to prevent further changes in H for that many steps. */
  /* ----------------------------------------------------------------------- */
  L100: dls001_3.ipup = dls001_3.miter;
  dls001_3.lmax = dls001_3.maxord + 1;
  if (dls001_3.ialth == 1)
  {
    dls001_3.ialth = 2;
  }
  if (dls001_3.meth == dlsa01_1.mused)
  {
    goto L160;
  }
  dcfode_(&dls001_3.meth, dls001_3.elco, dls001_3.tesco);
  dls001_3.ialth = dls001_3.l;
  iret = 1;
  /* ----------------------------------------------------------------------- */
  /* The el vector and related constants are reset */
  /* whenever the order NQ is changed, or at the start of the problem. */
  /* ----------------------------------------------------------------------- */
  L150: i__1 = dls001_3.l;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L155: */
    dls001_3.el[i__ - 1] = dls001_3.elco[i__ + dls001_3.nq * 13-14];
  }
  dls001_3.nqnyh = dls001_3.nq **nyh;
  dls001_3.rc = dls001_3.rc * dls001_3.el[0] / dls001_3.el0;
  dls001_3.el0 = dls001_3.el[0];
  dls001_3.conit = .5 / (dls001_3.nq + 2);
  switch (iret)
  {
    case 1:
      goto L160;
    case 2:
      goto L170;
    case 3:
      goto L200;
  }
  /* ----------------------------------------------------------------------- */
  /* If H is being changed, the H ratio RH is checked against */
  /* RMAX, HMIN, and HMXI, and the YH array rescaled.  IALTH is set to */
  /* L = NQ + 1 to prevent a change of H for that many steps, unless */
  /* forced by a convergence or error test failure. */
  /* ----------------------------------------------------------------------- */
  L160: if (dls001_3.h__ == dls001_3.hold)
  {
    goto L200;
  }
  rh = dls001_3.h__ / dls001_3.hold;
  dls001_3.h__ = dls001_3.hold;
  iredo = 3;
  goto L175;
  L170: 
  /* Computing MAX */
  d__1 = rh, d__2 = dls001_3.hmin / abs(dls001_3.h__);
  rh = max(d__1, d__2);
  L175: rh = min(rh, dls001_3.rmax);
  /* Computing MAX */
  d__1 = 1., d__2 = abs(dls001_3.h__) *dls001_3.hmxi * rh;
  rh /= max(d__1, d__2);
  /* ----------------------------------------------------------------------- */
  /* If METH = 1, also restrict the new step size by the stability region. */
  /* If this reduces H, set IRFLAG to 1 so that if there are roundoff */
  /* problems later, we can assume that is the cause of the trouble. */
  /* ----------------------------------------------------------------------- */
  if (dls001_3.meth == 2)
  {
    goto L178;
  }
  dlsa01_1.irflag = 0;
  /* Computing MAX */
  d__1 = abs(dls001_3.h__) *dlsa01_1.pdlast;
  pdh = max(d__1, 1e-6);
  if (rh *pdh * 1.00001 < sm1[dls001_3.nq - 1])
  {
    goto L178;
  }
  rh = sm1[dls001_3.nq - 1] / pdh;
  dlsa01_1.irflag = 1;
  L178: r__ = 1.;
  i__1 = dls001_3.l;
  for (j = 2; j <= i__1; ++j)
  {
    r__ *= rh;
    i__2 = dls001_3.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L180: */
      yh[i__ + j * yh_dim1] *= r__;
    }
  }
  dls001_3.h__ *= rh;
  dls001_3.rc *= rh;
  dls001_3.ialth = dls001_3.l;
  if (iredo == 0)
  {
    goto L690;
  }
  /* ----------------------------------------------------------------------- */
  /* This section computes the predicted values by effectively */
  /* multiplying the YH array by the Pascal triangle matrix. */
  /* RC is the ratio of new to old values of the coefficient  H*EL(1). */
  /* When RC differs from 1 by more than CCMAX, IPUP is set to MITER */
  /* to force PJAC to be called, if a Jacobian is involved. */
  /* In any case, PJAC is called at least every MSBP steps. */
  /* ----------------------------------------------------------------------- */
  L200: if ((d__1 = dls001_3.rc - 1., abs(d__1)) > dls001_3.ccmax)
  {
    dls001_3.ipup = dls001_3.miter;
  }
  if (dls001_3.nst >= dls001_3.nslp + dls001_3.msbp)
  {
    dls001_3.ipup = dls001_3.miter;
  }
  dls001_3.tn += dls001_3.h__;
  i1 = dls001_3.nqnyh + 1;
  i__2 = dls001_3.nq;
  for (jb = 1; jb <= i__2; ++jb)
  {
    i1 -=  *nyh;
    /* DIR$ IVDEP */
    i__1 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__1; ++i__)
    {
      /* L210: */
      yh1[i__] += yh1[i__ +  *nyh];
    }
    /* L215: */
  }
  pnorm = dmnorm_(&dls001_3.n, &yh1[1], &ewt[1]);
  /* ----------------------------------------------------------------------- */
  /* Up to MAXCOR corrector iterations are taken.  A convergence test is */
  /* made on the RMS-norm of each correction, weighted by the error */
  /* weight vector EWT.  The sum of the corrections is accumulated in the */
  /* vector ACOR(i).  The YH array is not altered in the corrector loop. */
  /* ----------------------------------------------------------------------- */
  L220: m = 0;
  rate = 0.;
  del = 0.;
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L230: */
    y[i__] = yh[i__ + yh_dim1];
  }
  (*f)(&neq[1], &dls001_3.tn, &y[1], &savf[1]);
  ++dls001_3.nfe;
  if (dls001_3.ipup <= 0)
  {
    goto L250;
  }
  /* ----------------------------------------------------------------------- */
  /* If indicated, the matrix P = I - H*EL(1)*J is reevaluated and */
  /* preprocessed before starting the corrector iteration.  IPUP is set */
  /* to 0 as an indicator that this has been done. */
  /* ----------------------------------------------------------------------- */
  (*pjac)(&neq[1], &y[1], &yh[yh_offset], nyh, &ewt[1], &acor[1], &savf[1], 
    &wm[1], &iwm[1], (S_fp)f, (U_fp)jac);
  dls001_3.ipup = 0;
  dls001_3.rc = 1.;
  dls001_3.nslp = dls001_3.nst;
  dls001_3.crate = .7;
  if (dls001_3.ierpj != 0)
  {
    goto L430;
  }
  L250: i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L260: */
    acor[i__] = 0.;
  }
  L270: if (dls001_3.miter != 0)
  {
    goto L350;
  }
  /* ----------------------------------------------------------------------- */
  /* In the case of functional iteration, update Y directly from */
  /* the result of the last function evaluation. */
  /* ----------------------------------------------------------------------- */
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    savf[i__] = dls001_3.h__ * savf[i__] - yh[i__ + (yh_dim1 << 1)];
    /* L290: */
    y[i__] = savf[i__] - acor[i__];
  }
  del = dmnorm_(&dls001_3.n, &y[1], &ewt[1]);
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    y[i__] = yh[i__ + yh_dim1] + dls001_3.el[0] *savf[i__];
    /* L300: */
    acor[i__] = savf[i__];
  }
  goto L400;
  /* ----------------------------------------------------------------------- */
  /* In the case of the chord method, compute the corrector error, */
  /* and solve the linear system with that as right-hand side and */
  /* P as coefficient matrix. */
  /* ----------------------------------------------------------------------- */
  L350: i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L360: */
    y[i__] = dls001_3.h__ * savf[i__] - (yh[i__ + (yh_dim1 << 1)] + acor[i__]);
  }
  (*slvs)(&wm[1], &iwm[1], &y[1], &savf[1]);
  if (dls001_3.iersl < 0)
  {
    goto L430;
  }
  if (dls001_3.iersl > 0)
  {
    goto L410;
  }
  del = dmnorm_(&dls001_3.n, &y[1], &ewt[1]);
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    acor[i__] += y[i__];
    /* L380: */
    y[i__] = yh[i__ + yh_dim1] + dls001_3.el[0] *acor[i__];
  }
  /* ----------------------------------------------------------------------- */
  /* Test for convergence.  If M .gt. 0, an estimate of the convergence */
  /* rate constant is stored in CRATE, and this is used in the test. */

  /* We first check for a change of iterates that is the size of */
  /* roundoff error.  If this occurs, the iteration has converged, and a */
  /* new rate estimate is not formed. */
  /* In all other cases, force at least two iterations to estimate a */
  /* local Lipschitz constant estimate for Adams methods. */
  /* On convergence, form PDEST = local maximum Lipschitz constant */
  /* estimate.  PDLAST is the most recent nonzero estimate. */
  /* ----------------------------------------------------------------------- */
  L400: if (del <= pnorm *100. * dls001_3.uround)
  {
    goto L450;
  }
  if (m == 0 && dls001_3.meth == 1)
  {
    goto L405;
  }
  if (m == 0)
  {
    goto L402;
  }
  rm = 1024.;
  if (del <= delp *1024.)
  {
    rm = del / delp;
  }
  rate = max(rate, rm);
  /* Computing MAX */
  d__1 = dls001_3.crate * .2;
  dls001_3.crate = max(d__1, rm);
  L402: 
  /* Computing MIN */
  d__1 = 1., d__2 = dls001_3.crate * 1.5;
  dcon = del * min(d__1, d__2) / (dls001_3.tesco[dls001_3.nq *3-2]
    *dls001_3.conit);
  if (dcon > 1.)
  {
    goto L405;
  }
  /* Computing MAX */
  d__2 = dlsa01_1.pdest, d__3 = rate / (d__1 = dls001_3.h__ * dls001_3.el[0],
    abs(d__1));
  dlsa01_1.pdest = max(d__2, d__3);
  if (dlsa01_1.pdest != 0.)
  {
    dlsa01_1.pdlast = dlsa01_1.pdest;
  }
  goto L450;
  L405: ++m;
  if (m == dls001_3.maxcor)
  {
    goto L410;
  }
  if (m >= 2 && del > delp *2.)
  {
    goto L410;
  }
  delp = del;
  (*f)(&neq[1], &dls001_3.tn, &y[1], &savf[1]);
  ++dls001_3.nfe;
  goto L270;
  /* ----------------------------------------------------------------------- */
  /* The corrector iteration failed to converge. */
  /* If MITER .ne. 0 and the Jacobian is out of date, PJAC is called for */
  /* the next try.  Otherwise the YH array is retracted to its values */
  /* before prediction, and H is reduced, if possible.  If H cannot be */
  /* reduced or MXNCF failures have occurred, exit with KFLAG = -2. */
  /* ----------------------------------------------------------------------- */
  L410: if (dls001_3.miter == 0 || dls001_3.jcur == 1)
  {
    goto L430;
  }
  dls001_3.icf = 1;
  dls001_3.ipup = dls001_3.miter;
  goto L220;
  L430: dls001_3.icf = 2;
  ++ncf;
  dls001_3.rmax = 2.;
  dls001_3.tn = told;
  i1 = dls001_3.nqnyh + 1;
  i__2 = dls001_3.nq;
  for (jb = 1; jb <= i__2; ++jb)
  {
    i1 -=  *nyh;
    /* DIR$ IVDEP */
    i__1 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__1; ++i__)
    {
      /* L440: */
      yh1[i__] -= yh1[i__ +  *nyh];
    }
    /* L445: */
  }
  if (dls001_3.ierpj < 0 || dls001_3.iersl < 0)
  {
    goto L680;
  }
  if (abs(dls001_3.h__) <= dls001_3.hmin *1.00001)
  {
    goto L670;
  }
  if (ncf == dls001_3.mxncf)
  {
    goto L670;
  }
  rh = .25;
  dls001_3.ipup = dls001_3.miter;
  iredo = 1;
  goto L170;
  /* ----------------------------------------------------------------------- */
  /* The corrector has converged.  JCUR is set to 0 */
  /* to signal that the Jacobian involved may need updating later. */
  /* The local error test is made and control passes to statement 500 */
  /* if it fails. */
  /* ----------------------------------------------------------------------- */
  L450: dls001_3.jcur = 0;
  if (m == 0)
  {
    dsm = del / dls001_3.tesco[dls001_3.nq *3-2];
  }
  if (m > 0)
  {
    dsm = dmnorm_(&dls001_3.n, &acor[1], &ewt[1]) / dls001_3.tesco[dls001_3.nq
      *3-2];
  }
  if (dsm > 1.)
  {
    goto L500;
  }
  /* ----------------------------------------------------------------------- */
  /* After a successful step, update the YH array. */
  /* Decrease ICOUNT by 1, and if it is -1, consider switching methods. */
  /* If a method switch is made, reset various parameters, */
  /* rescale the YH array, and exit.  If there is no switch, */
  /* consider changing H if IALTH = 1.  Otherwise decrease IALTH by 1. */
  /* If IALTH is then 1 and NQ .lt. MAXORD, then ACOR is saved for */
  /* use in a possible order increase on the next step. */
  /* If a change in H is considered, an increase or decrease in order */
  /* by one is considered also.  A change in H is made only if it is by a */
  /* factor of at least 1.1.  If not, IALTH is set to 3 to prevent */
  /* testing for that many steps. */
  /* ----------------------------------------------------------------------- */
  dls001_3.kflag = 0;
  iredo = 0;
  ++dls001_3.nst;
  dls001_3.hu = dls001_3.h__;
  dls001_3.nqu = dls001_3.nq;
  dlsa01_1.mused = dls001_3.meth;
  i__2 = dls001_3.l;
  for (j = 1; j <= i__2; ++j)
  {
    i__1 = dls001_3.n;
    for (i__ = 1; i__ <= i__1; ++i__)
    {
      /* L460: */
      yh[i__ + j * yh_dim1] += dls001_3.el[j - 1] *acor[i__];
    }
  }
  --dlsa01_1.icount;
  if (dlsa01_1.icount >= 0)
  {
    goto L488;
  }
  if (dls001_3.meth == 2)
  {
    goto L480;
  }
  /* ----------------------------------------------------------------------- */
  /* We are currently using an Adams method.  Consider switching to BDF. */
  /* If the current order is greater than 5, assume the problem is */
  /* not stiff, and skip this section. */
  /* If the Lipschitz constant and error estimate are not polluted */
  /* by roundoff, go to 470 and perform the usual test. */
  /* Otherwise, switch to the BDF methods if the last step was */
  /* restricted to insure stability (irflag = 1), and stay with Adams */
  /* method if not.  When switching to BDF with polluted error estimates, */
  /* in the absence of other information, double the step size. */

  /* When the estimates are OK, we make the usual test by computing */
  /* the step size we could have (ideally) used on this step, */
  /* with the current (Adams) method, and also that for the BDF. */
  /* If NQ .gt. MXORDS, we consider changing to order MXORDS on switching. */
  /* Compare the two step sizes to decide whether to switch. */
  /* The step size advantage must be at least RATIO = 5 to switch. */
  /* ----------------------------------------------------------------------- */
  if (dls001_3.nq > 5)
  {
    goto L488;
  }
  if (dsm > pnorm *100. * dls001_3.uround && dlsa01_1.pdest != 0.)
  {
    goto L470;
  }
  if (dlsa01_1.irflag == 0)
  {
    goto L488;
  }
  rh2 = 2.;
  nqm2 = min(dls001_3.nq, dlsa01_1.mxords);
  goto L478;
  L470: exsm = 1. / dls001_3.l;
  rh1 = 1. / (pow_dd(&dsm, &exsm) *1.2 + 1.2e-6);
  rh1it = rh1 * 2.;
  pdh = dlsa01_1.pdlast * abs(dls001_3.h__);
  if (pdh *rh1 > 1e-5)
  {
    rh1it = sm1[dls001_3.nq - 1] / pdh;
  }
  rh1 = min(rh1, rh1it);
  if (dls001_3.nq <= dlsa01_1.mxords)
  {
    goto L474;
  }
  nqm2 = dlsa01_1.mxords;
  lm2 = dlsa01_1.mxords + 1;
  exm2 = 1. / lm2;
  lm2p1 = lm2 + 1;
  dm2 = dmnorm_(&dls001_3.n, &yh[lm2p1 *yh_dim1 + 1], &ewt[1]) /
    dlsa01_1.cm2[dlsa01_1.mxords - 1];
  rh2 = 1. / (pow_dd(&dm2, &exm2) *1.2 + 1.2e-6);
  goto L476;
  L474: dm2 = dsm *(dlsa01_1.cm1[dls001_3.nq - 1] / dlsa01_1.cm2[dls001_3.nq -
    1]);
  rh2 = 1. / (pow_dd(&dm2, &exsm) *1.2 + 1.2e-6);
  nqm2 = dls001_3.nq;
  L476: if (rh2 < dlsa01_1.ratio *rh1)
  {
    goto L488;
  }
  /* THE SWITCH TEST PASSED.  RESET RELEVANT QUANTITIES FOR BDF. ---------- */
  L478: rh = rh2;
  dlsa01_1.icount = 20;
  dls001_3.meth = 2;
  dls001_3.miter = dlsa01_1.jtyp;
  dlsa01_1.pdlast = 0.;
  dls001_3.nq = nqm2;
  dls001_3.l = dls001_3.nq + 1;
  goto L170;
  /* ----------------------------------------------------------------------- */
  /* We are currently using a BDF method.  Consider switching to Adams. */
  /* Compute the step size we could have (ideally) used on this step, */
  /* with the current (BDF) method, and also that for the Adams. */
  /* If NQ .gt. MXORDN, we consider changing to order MXORDN on switching. */
  /* Compare the two step sizes to decide whether to switch. */
  /* The step size advantage must be at least 5/RATIO = 1 to switch. */
  /* If the step size for Adams would be so small as to cause */
  /* roundoff pollution, we stay with BDF. */
  /* ----------------------------------------------------------------------- */
  L480: exsm = 1. / dls001_3.l;
  if (dlsa01_1.mxordn >= dls001_3.nq)
  {
    goto L484;
  }
  nqm1 = dlsa01_1.mxordn;
  lm1 = dlsa01_1.mxordn + 1;
  exm1 = 1. / lm1;
  lm1p1 = lm1 + 1;
  dm1 = dmnorm_(&dls001_3.n, &yh[lm1p1 *yh_dim1 + 1], &ewt[1]) /
    dlsa01_1.cm1[dlsa01_1.mxordn - 1];
  rh1 = 1. / (pow_dd(&dm1, &exm1) *1.2 + 1.2e-6);
  goto L486;
  L484: dm1 = dsm *(dlsa01_1.cm2[dls001_3.nq - 1] / dlsa01_1.cm1[dls001_3.nq -
    1]);
  rh1 = 1. / (pow_dd(&dm1, &exsm) *1.2 + 1.2e-6);
  nqm1 = dls001_3.nq;
  exm1 = exsm;
  L486: rh1it = rh1 * 2.;
  pdh = dlsa01_1.pdnorm * abs(dls001_3.h__);
  if (pdh *rh1 > 1e-5)
  {
    rh1it = sm1[nqm1 - 1] / pdh;
  }
  rh1 = min(rh1, rh1it);
  rh2 = 1. / (pow_dd(&dsm, &exsm) *1.2 + 1.2e-6);
  if (rh1 *dlsa01_1.ratio < rh2 *5.)
  {
    goto L488;
  }
  alpha = max(.001, rh1);
  dm1 = pow_dd(&alpha, &exm1) *dm1;
  if (dm1 <= dls001_3.uround *1e3 * pnorm)
  {
    goto L488;
  }
  /* The switch test passed.  Reset relevant quantities for Adams. -------- */
  rh = rh1;
  dlsa01_1.icount = 20;
  dls001_3.meth = 1;
  dls001_3.miter = 0;
  dlsa01_1.pdlast = 0.;
  dls001_3.nq = nqm1;
  dls001_3.l = dls001_3.nq + 1;
  goto L170;

  /* No method switch is being made.  Do the usual step/order selection. -- */
  L488: --dls001_3.ialth;
  if (dls001_3.ialth == 0)
  {
    goto L520;
  }
  if (dls001_3.ialth > 1)
  {
    goto L700;
  }
  if (dls001_3.l == dls001_3.lmax)
  {
    goto L700;
  }
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L490: */
    yh[i__ + dls001_3.lmax * yh_dim1] = acor[i__];
  }
  goto L700;
  /* ----------------------------------------------------------------------- */
  /* The error test failed.  KFLAG keeps track of multiple failures. */
  /* Restore TN and the YH array to their previous values, and prepare */
  /* to try the step again.  Compute the optimum step size for this or */
  /* one lower order.  After 2 or more failures, H is forced to decrease */
  /* by a factor of 0.2 or less. */
  /* ----------------------------------------------------------------------- */
  L500: --dls001_3.kflag;
  dls001_3.tn = told;
  i1 = dls001_3.nqnyh + 1;
  i__1 = dls001_3.nq;
  for (jb = 1; jb <= i__1; ++jb)
  {
    i1 -=  *nyh;
    /* DIR$ IVDEP */
    i__2 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__2; ++i__)
    {
      /* L510: */
      yh1[i__] -= yh1[i__ +  *nyh];
    }
    /* L515: */
  }
  dls001_3.rmax = 2.;
  if (abs(dls001_3.h__) <= dls001_3.hmin *1.00001)
  {
    goto L660;
  }
  if (dls001_3.kflag <=  - 3)
  {
    goto L640;
  }
  iredo = 2;
  rhup = 0.;
  goto L540;
  /* ----------------------------------------------------------------------- */
  /* Regardless of the success or failure of the step, factors */
  /* RHDN, RHSM, and RHUP are computed, by which H could be multiplied */
  /* at order NQ - 1, order NQ, or order NQ + 1, respectively. */
  /* In the case of failure, RHUP = 0.0 to avoid an order increase. */
  /* The largest of these is determined and the new order chosen */
  /* accordingly.  If the order is to be increased, we compute one */
  /* additional scaled derivative. */
  /* ----------------------------------------------------------------------- */
  L520: rhup = 0.;
  if (dls001_3.l == dls001_3.lmax)
  {
    goto L540;
  }
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L530: */
    savf[i__] = acor[i__] - yh[i__ + dls001_3.lmax * yh_dim1];
  }
  dup = dmnorm_(&dls001_3.n, &savf[1], &ewt[1]) / dls001_3.tesco[dls001_3.nq *3
    -1];
  exup = 1. / (dls001_3.l + 1);
  rhup = 1. / (pow_dd(&dup, &exup) *1.4 + 1.4e-6);
  L540: exsm = 1. / dls001_3.l;
  rhsm = 1. / (pow_dd(&dsm, &exsm) *1.2 + 1.2e-6);
  rhdn = 0.;
  if (dls001_3.nq == 1)
  {
    goto L550;
  }
  ddn = dmnorm_(&dls001_3.n, &yh[dls001_3.l *yh_dim1 + 1], &ewt[1]) /
    dls001_3.tesco[dls001_3.nq *3-3];
  exdn = 1. / dls001_3.nq;
  rhdn = 1. / (pow_dd(&ddn, &exdn) *1.3 + 1.3e-6);
  /* If METH = 1, limit RH according to the stability region also. -------- */
  L550: if (dls001_3.meth == 2)
  {
    goto L560;
  }
  /* Computing MAX */
  d__1 = abs(dls001_3.h__) *dlsa01_1.pdlast;
  pdh = max(d__1, 1e-6);
  if (dls001_3.l < dls001_3.lmax)
  {
    /* Computing MIN */
    d__1 = rhup, d__2 = sm1[dls001_3.l - 1] / pdh;
    rhup = min(d__1, d__2);
  }
  /* Computing MIN */
  d__1 = rhsm, d__2 = sm1[dls001_3.nq - 1] / pdh;
  rhsm = min(d__1, d__2);
  if (dls001_3.nq > 1)
  {
    /* Computing MIN */
    d__1 = rhdn, d__2 = sm1[dls001_3.nq - 2] / pdh;
    rhdn = min(d__1, d__2);
  }
  dlsa01_1.pdest = 0.;
  L560: if (rhsm >= rhup)
  {
    goto L570;
  }
  if (rhup > rhdn)
  {
    goto L590;
  }
  goto L580;
  L570: if (rhsm < rhdn)
  {
    goto L580;
  }
  newq = dls001_3.nq;
  rh = rhsm;
  goto L620;
  L580: newq = dls001_3.nq - 1;
  rh = rhdn;
  if (dls001_3.kflag < 0 && rh > 1.)
  {
    rh = 1.;
  }
  goto L620;
  L590: newq = dls001_3.l;
  rh = rhup;
  if (rh < 1.1)
  {
    goto L610;
  }
  r__ = dls001_3.el[dls001_3.l - 1] / dls001_3.l;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L600: */
    yh[i__ + (newq + 1) *yh_dim1] = acor[i__] *r__;
  }
  goto L630;
  L610: dls001_3.ialth = 3;
  goto L700;
  /* If METH = 1 and H is restricted by stability, bypass 10 percent test. */
  L620: if (dls001_3.meth == 2)
  {
    goto L622;
  }
  if (rh *pdh * 1.00001 >= sm1[newq - 1])
  {
    goto L625;
  }
  L622: if (dls001_3.kflag == 0 && rh < 1.1)
  {
    goto L610;
  }
  L625: if (dls001_3.kflag <=  - 2)
  {
    rh = min(rh, .2);
  }
  /* ----------------------------------------------------------------------- */
  /* If there is a change of order, reset NQ, L, and the coefficients. */
  /* In any case H is reset according to RH and the YH array is rescaled. */
  /* Then exit from 690 if the step was OK, or redo the step otherwise. */
  /* ----------------------------------------------------------------------- */
  if (newq == dls001_3.nq)
  {
    goto L170;
  }
  L630: dls001_3.nq = newq;
  dls001_3.l = dls001_3.nq + 1;
  iret = 2;
  goto L150;
  /* ----------------------------------------------------------------------- */
  /* Control reaches this section if 3 or more failures have occured. */
  /* If 10 failures have occurred, exit with KFLAG = -1. */
  /* It is assumed that the derivatives that have accumulated in the */
  /* YH array have errors of the wrong order.  Hence the first */
  /* derivative is recomputed, and the order is set to 1.  Then */
  /* H is reduced by a factor of 10, and the step is retried, */
  /* until it succeeds or H reaches HMIN. */
  /* ----------------------------------------------------------------------- */
  L640: if (dls001_3.kflag ==  - 10)
  {
    goto L660;
  }
  rh = .1;
  /* Computing MAX */
  d__1 = dls001_3.hmin / abs(dls001_3.h__);
  rh = max(d__1, rh);
  dls001_3.h__ *= rh;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L645: */
    y[i__] = yh[i__ + yh_dim1];
  }
  (*f)(&neq[1], &dls001_3.tn, &y[1], &savf[1]);
  ++dls001_3.nfe;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L650: */
    yh[i__ + (yh_dim1 << 1)] = dls001_3.h__ * savf[i__];
  }
  dls001_3.ipup = dls001_3.miter;
  dls001_3.ialth = 5;
  if (dls001_3.nq == 1)
  {
    goto L200;
  }
  dls001_3.nq = 1;
  dls001_3.l = 2;
  iret = 3;
  goto L150;
  /* ----------------------------------------------------------------------- */
  /* All returns are made through this section.  H is saved in HOLD */
  /* to allow the caller to change H on the next step. */
  /* ----------------------------------------------------------------------- */
  L660: dls001_3.kflag =  - 1;
  goto L720;
  L670: dls001_3.kflag =  - 2;
  goto L720;
  L680: dls001_3.kflag =  - 3;
  goto L720;
  L690: dls001_3.rmax = 10.;
  L700: r__ = 1. / dls001_3.tesco[dls001_3.nqu *3-2];
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L710: */
    acor[i__] *= r__;
  }
  L720: dls001_3.hold = dls001_3.h__;
  dls001_3.jstart = 1;
  return 0;
  /* ----------------------- End of Subroutine DSTODA ---------------------- */
} /* dstoda_ */

/* DECK DPRJA */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dprja_
   (integer *neq, doublereal *y, doublereal *yh, integer *nyh, doublereal *ewt,
   doublereal *ftem, doublereal *savf, doublereal *wm, integer *iwm, S_fp f,
   S_fp jac)
{
  /* System generated locals */
  integer yh_dim1, yh_offset, i__1, i__2, i__3, i__4;
  doublereal d__1, d__2;

  /* Local variables */
  static integer i__, j;
  static doublereal r__;
  static integer i1, i2, j1;
  static doublereal r0;
  static integer ii, jj, ml, mu;
  static doublereal yi, yj, hl0;
  static integer ml3, np1;
  static doublereal fac;
  static integer mba, ier;
  static doublereal con, yjj;
  static integer meb1, lenp;
  static doublereal srur;
  static integer mband, meband;

  /* ----------------------------------------------------------------------- */
  /* DPRJA is called by DSTODA to compute and process the matrix */
  /* P = I - H*EL(1)*J , where J is an approximation to the Jacobian. */
  /* Here J is computed by the user-supplied routine JAC if */
  /* MITER = 1 or 4 or by finite differencing if MITER = 2 or 5. */
  /* J, scaled by -H*EL(1), is stored in WM.  Then the norm of J (the */
  /* matrix norm consistent with the weighted max-norm on vectors given */
  /* by DMNORM) is computed, and J is overwritten by P.  P is then */
  /* subjected to LU decomposition in preparation for later solution */
  /* of linear systems with P as coefficient matrix.  This is done */
  /* by DGEFA if MITER = 1 or 2, and by DGBFA if MITER = 4 or 5. */

  /* In addition to variables described previously, communication */
  /* with DPRJA uses the following: */
  /* Y     = array containing predicted values on entry. */
  /* FTEM  = work array of length N (ACOR in DSTODA). */
  /* SAVF  = array containing f evaluated at predicted y. */
  /* WM    = real work space for matrices.  On output it contains the */
  /*         LU decomposition of P. */
  /*         Storage of matrix elements starts at WM(3). */
  /*         WM also contains the following matrix-related data: */
  /*         WM(1) = SQRT(UROUND), used in numerical Jacobian increments. */
  /* IWM   = integer work space containing pivot information, starting at */
  /*         IWM(21).   IWM also contains the band parameters */
  /*         ML = IWM(1) and MU = IWM(2) if MITER is 4 or 5. */
  /* EL0   = EL(1) (input). */
  /* PDNORM= norm of Jacobian matrix. (Output). */
  /* IERPJ = output error flag,  = 0 if no trouble, .gt. 0 if */
  /*         P matrix found to be singular. */
  /* JCUR  = output flag = 1 to indicate that the Jacobian matrix */
  /*         (or approximation) is now current. */
  /* This routine also uses the Common variables EL0, H, TN, UROUND, */
  /* MITER, N, NFE, and NJE. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --neq;
  --y;
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --ewt;
  --ftem;
  --savf;
  --wm;
  --iwm;

  /* Function Body */
  ++dls001_1.nje;
  dls001_1.ierpj = 0;
  dls001_1.jcur = 1;
  hl0 = dls001_1.h__ * dls001_1.el0;
  switch (dls001_1.miter)
  {
    case 1:
      goto L100;
    case 2:
      goto L200;
    case 3:
      goto L300;
    case 4:
      goto L400;
    case 5:
      goto L500;
  }
  /* If MITER = 1, call JAC and multiply by scalar. ----------------------- */
  L100: lenp = dls001_1.n * dls001_1.n;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L110: */
    wm[i__ + 2] = 0.;
  }
  (*jac)(&neq[1], &dls001_1.tn, &y[1], &c__0, &c__0, &wm[3], &dls001_1.n);
  con =  - hl0;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L120: */
    wm[i__ + 2] *= con;
  }
  goto L240;
  /* If MITER = 2, make N calls to F to approximate J. -------------------- */
  L200: fac = dmnorm_(&dls001_1.n, &savf[1], &ewt[1]);
  r0 = abs(dls001_1.h__) *1e3 * dls001_1.uround * dls001_1.n * fac;
  if (r0 == 0.)
  {
    r0 = 1.;
  }
  srur = wm[1];
  j1 = 2;
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    yj = y[j];
    /* Computing MAX */
    d__1 = srur * abs(yj), d__2 = r0 / ewt[j];
    r__ = max(d__1, d__2);
    y[j] += r__;
    fac =  - hl0 / r__;
    (*f)(&neq[1], &dls001_1.tn, &y[1], &ftem[1]);
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L220: */
      wm[i__ + j1] = (ftem[i__] - savf[i__]) *fac;
    }
    y[j] = yj;
    j1 += dls001_1.n;
    /* L230: */
  }
  dls001_1.nfe += dls001_1.n;
  L240: 
  /* Compute norm of Jacobian. -------------------------------------------- */
  dlsa01_2.pdnorm = dfnorm_(&dls001_1.n, &wm[3], &ewt[1]) / abs(hl0);
  /* Add identity matrix. ------------------------------------------------- */
  j = 3;
  np1 = dls001_1.n + 1;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    wm[j] += 1.;
    /* L250: */
    j += np1;
  }
  /* Do LU decomposition on P. -------------------------------------------- */
  dgefa_(&wm[3], &dls001_1.n, &dls001_1.n, &iwm[21], &ier);
  if (ier != 0)
  {
    dls001_1.ierpj = 1;
  }
  return 0;
  /* Dummy block only, since MITER is never 3 in this routine. ------------ */
  L300: return 0;
  /* If MITER = 4, call JAC and multiply by scalar. ----------------------- */
  L400: ml = iwm[1];
  mu = iwm[2];
  ml3 = ml + 3;
  mband = ml + mu + 1;
  meband = mband + ml;
  lenp = meband * dls001_1.n;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L410: */
    wm[i__ + 2] = 0.;
  }
  (*jac)(&neq[1], &dls001_1.tn, &y[1], &ml, &mu, &wm[ml3], &meband);
  con =  - hl0;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L420: */
    wm[i__ + 2] *= con;
  }
  goto L570;
  /* If MITER = 5, make MBAND calls to F to approximate J. ---------------- */
  L500: ml = iwm[1];
  mu = iwm[2];
  mband = ml + mu + 1;
  mba = min(mband, dls001_1.n);
  meband = mband + ml;
  meb1 = meband - 1;
  srur = wm[1];
  fac = dmnorm_(&dls001_1.n, &savf[1], &ewt[1]);
  r0 = abs(dls001_1.h__) *1e3 * dls001_1.uround * dls001_1.n * fac;
  if (r0 == 0.)
  {
    r0 = 1.;
  }
  i__1 = mba;
  for (j = 1; j <= i__1; ++j)
  {
    i__2 = dls001_1.n;
    i__3 = mband;
    for (i__ = j; i__3 < 0 ? i__ >= i__2: i__ <= i__2; i__ += i__3)
    {
      yi = y[i__];
      /* Computing MAX */
      d__1 = srur * abs(yi), d__2 = r0 / ewt[i__];
      r__ = max(d__1, d__2);
      /* L530: */
      y[i__] += r__;
    }
    (*f)(&neq[1], &dls001_1.tn, &y[1], &ftem[1]);
    i__3 = dls001_1.n;
    i__2 = mband;
    for (jj = j; i__2 < 0 ? jj >= i__3: jj <= i__3; jj += i__2)
    {
      y[jj] = yh[jj + yh_dim1];
      yjj = y[jj];
      /* Computing MAX */
      d__1 = srur * abs(yjj), d__2 = r0 / ewt[jj];
      r__ = max(d__1, d__2);
      fac =  - hl0 / r__;
      /* Computing MAX */
      i__4 = jj - mu;
      i1 = max(i__4, 1);
      /* Computing MIN */
      i__4 = jj + ml;
      i2 = min(i__4, dls001_1.n);
      ii = jj * meb1 - ml + 2;
      i__4 = i2;
      for (i__ = i1; i__ <= i__4; ++i__)
      {
        /* L540: */
        wm[ii + i__] = (ftem[i__] - savf[i__]) *fac;
      }
      /* L550: */
    }
    /* L560: */
  }
  dls001_1.nfe += mba;
  L570: 
  /* Compute norm of Jacobian. -------------------------------------------- */
  dlsa01_2.pdnorm = dbnorm_(&dls001_1.n, &wm[ml + 3], &meband, &ml, &mu,
    &ewt[1]) / abs(hl0);
  /* Add identity matrix. ------------------------------------------------- */
  ii = mband + 2;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    wm[ii] += 1.;
    /* L580: */
    ii += meband;
  }
  /* Do LU decomposition of P. -------------------------------------------- */
  dgbfa_(&wm[3], &meband, &dls001_1.n, &ml, &mu, &iwm[21], &ier);
  if (ier != 0)
  {
    dls001_1.ierpj = 1;
  }
  return 0;
  /* ----------------------- End of Subroutine DPRJA ----------------------- */
} /* dprja_ */

/* DECK DMNORM */
doublereal SmartMathLibrary::OdePack::ManagedOdePackProvider::dmnorm_(integer
  *n, doublereal *v, doublereal *w)
{
  /* System generated locals */
  integer i__1;
  doublereal ret_val, d__1, d__2, d__3;

  /* Local variables */
  static integer i__;
  static doublereal vm;

  /* ----------------------------------------------------------------------- */
  /* This function routine computes the weighted max-norm */
  /* of the vector of length N contained in the array V, with weights */
  /* contained in the array w of length N: */
  /*   DMNORM = MAX(i=1,...,N) ABS(V(i))*W(i) */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --w;
  --v;

  /* Function Body */
  vm = 0.;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    /* Computing MAX */
    d__2 = vm, d__3 = (d__1 = v[i__], abs(d__1)) *w[i__];
    vm = max(d__2, d__3);
  }
  ret_val = vm;
  return ret_val;
  /* ----------------------- End of Function DMNORM ------------------------ */
} /* dmnorm_ */

/* DECK DFNORM */
doublereal SmartMathLibrary::OdePack::ManagedOdePackProvider::dfnorm_(integer
  *n, doublereal *a, doublereal *w)
{
  /* System generated locals */
  integer a_dim1, a_offset, i__1, i__2;
  doublereal ret_val, d__1, d__2;

  /* Local variables */
  static integer i__, j;
  static doublereal an, sum;

  /* ----------------------------------------------------------------------- */
  /* This function computes the norm of a full N by N matrix, */
  /* stored in the array A, that is consistent with the weighted max-norm */
  /* on vectors, with weights stored in the array W: */
  /*   DFNORM = MAX(i=1,...,N) ( W(i) * Sum(j=1,...,N) ABS(a(i,j))/W(j) ) */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --w;
  a_dim1 =  *n;
  a_offset = 1+a_dim1;
  a -= a_offset;

  /* Function Body */
  an = 0.;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    sum = 0.;
    i__2 =  *n;
    for (j = 1; j <= i__2; ++j)
    {
      /* L10: */
      sum += (d__1 = a[i__ + j * a_dim1], abs(d__1)) / w[j];
    }
    /* Computing MAX */
    d__1 = an, d__2 = sum * w[i__];
    an = max(d__1, d__2);
    /* L20: */
  }
  ret_val = an;
  return ret_val;
  /* ----------------------- End of Function DFNORM ------------------------ */
} /* dfnorm_ */

/* DECK DBNORM */
doublereal SmartMathLibrary::OdePack::ManagedOdePackProvider::dbnorm_(integer
  *n, doublereal *a, integer *nra, integer *ml, integer *mu, doublereal *w)
{
  /* System generated locals */
  integer a_dim1, a_offset, i__1, i__2;
  doublereal ret_val, d__1, d__2;

  /* Local variables */
  static integer i__, j, i1;
  static doublereal an;
  static integer jhi, jlo;
  static doublereal sum;

  /* ----------------------------------------------------------------------- */
  /* This function computes the norm of a banded N by N matrix, */
  /* stored in the array A, that is consistent with the weighted max-norm */
  /* on vectors, with weights stored in the array W. */
  /* ML and MU are the lower and upper half-bandwidths of the matrix. */
  /* NRA is the first dimension of the A array, NRA .ge. ML+MU+1. */
  /* In terms of the matrix elements a(i,j), the norm is given by: */
  /*   DBNORM = MAX(i=1,...,N) ( W(i) * Sum(j=1,...,N) ABS(a(i,j))/W(j) ) */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --w;
  a_dim1 =  *nra;
  a_offset = 1+a_dim1;
  a -= a_offset;

  /* Function Body */
  an = 0.;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    sum = 0.;
    i1 = i__ +  *mu + 1;
    /* Computing MAX */
    i__2 = i__ -  *ml;
    jlo = max(i__2, 1);
    /* Computing MIN */
    i__2 = i__ +  *mu;
    jhi = min(i__2,  *n);
    i__2 = jhi;
    for (j = jlo; j <= i__2; ++j)
    {
      /* L10: */
      sum += (d__1 = a[i1 - j + j * a_dim1], abs(d__1)) / w[j];
    }
    /* Computing MAX */
    d__1 = an, d__2 = sum * w[i__];
    an = max(d__1, d__2);
    /* L20: */
  }
  ret_val = an;
  return ret_val;
  /* ----------------------- End of Function DBNORM ------------------------ */
} /* dbnorm_ */

/* DECK DSRCMA */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dsrcma_
   (doublereal *rsav, integer *isav, integer *job)
{
  /* Initialized data */

  static integer lenrls = 218;
  static integer lenils = 37;
  static integer lenrla = 22;
  static integer lenila = 9;

  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__;

  /* ----------------------------------------------------------------------- */
  /* This routine saves or restores (depending on JOB) the contents of */
  /* the Common blocks DLS001, DLSA01, which are used */
  /* internally by one or more ODEPACK solvers. */

  /* RSAV = real array of length 240 or more. */
  /* ISAV = integer array of length 46 or more. */
  /* JOB  = flag indicating to save or restore the Common blocks: */
  /*        JOB  = 1 if Common is to be saved (written to RSAV/ISAV) */
  /*        JOB  = 2 if Common is to be restored (read from RSAV/ISAV) */
  /*        A call with JOB = 2 presumes a prior call with JOB = 1. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --isav;
  --rsav;

  /* Function Body */

  if (*job == 2)
  {
    goto L100;
  }
  i__1 = lenrls;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    rsav[i__] = dls001_2.rls[i__ - 1];
  }
  i__1 = lenrla;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L15: */
    rsav[lenrls + i__] = dlsa01_3.rlsa[i__ - 1];
  }

  i__1 = lenils;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L20: */
    isav[i__] = dls001_2.ils[i__ - 1];
  }
  i__1 = lenila;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L25: */
    isav[lenils + i__] = dlsa01_3.ilsa[i__ - 1];
  }

  return 0;

  L100: i__1 = lenrls;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L110: */
    dls001_2.rls[i__ - 1] = rsav[i__];
  }
  i__1 = lenrla;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L115: */
    dlsa01_3.rlsa[i__ - 1] = rsav[lenrls + i__];
  }

  i__1 = lenils;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L120: */
    dls001_2.ils[i__ - 1] = isav[i__];
  }
  i__1 = lenila;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L125: */
    dlsa01_3.ilsa[i__ - 1] = isav[lenils + i__];
  }

  return 0;
  /* ----------------------- End of Subroutine DSRCMA ---------------------- */
} /* dsrcma_ */

/* DECK DRCHEK */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::drchek_
   (integer *job, S_fp g, integer *neq, doublereal *y, doublereal *yh, integer
   *nyh, doublereal *g0, doublereal *g1, doublereal *gx, integer *jroot,
   integer *irt)
{
  /* System generated locals */
  integer yh_dim1, yh_offset, i__1;
  doublereal d__1;

  /* Local variables */
  static integer i__;
  static doublereal x, t1, temp1, temp2;
  static integer iflag, jflag;
  static doublereal hming;
  static logical zroot;

  /* ----------------------------------------------------------------------- */
  /* This routine checks for the presence of a root in the vicinity of */
  /* the current T, in a manner depending on the input flag JOB.  It calls */
  /* Subroutine DROOTS to locate the root as precisely as possible. */

  /* In addition to variables described previously, DRCHEK */
  /* uses the following for communication: */
  /* JOB    = integer flag indicating type of call: */
  /*          JOB = 1 means the problem is being initialized, and DRCHEK */
  /*                  is to look for a root at or very near the initial T. */
  /*          JOB = 2 means a continuation call to the solver was just */
  /*                  made, and DRCHEK is to check for a root in the */
  /*                  relevant part of the step last taken. */
  /*          JOB = 3 means a successful step was just taken, and DRCHEK */
  /*                  is to look for a root in the interval of the step. */
  /* G0     = array of length NG, containing the value of g at T = T0. */
  /*          G0 is input for JOB .ge. 2, and output in all cases. */
  /* G1,GX  = arrays of length NG for work space. */
  /* IRT    = completion flag: */
  /*          IRT = 0  means no root was found. */
  /*          IRT = -1 means JOB = 1 and a root was found too near to T. */
  /*          IRT = 1  means a legitimate root was found (JOB = 2 or 3). */
  /*                   On return, T0 is the root location, and Y is the */
  /*                   corresponding solution vector. */
  /* T0     = value of T at one endpoint of interval of interest.  Only */
  /*          roots beyond T0 in the direction of integration are sought. */
  /*          T0 is input if JOB .ge. 2, and output in all cases. */
  /*          T0 is updated by DRCHEK, whether a root is found or not. */
  /* TLAST  = last value of T returned by the solver (input only). */
  /* TOUTC  = copy of TOUT (input only). */
  /* IRFND  = input flag showing whether the last step taken had a root. */
  /*          IRFND = 1 if it did, = 0 if not. */
  /* ITASKC = copy of ITASK (input only). */
  /* NGC    = copy of NG (input only). */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --neq;
  --y;
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --g0;
  --g1;
  --gx;
  --jroot;

  /* Function Body */
  *irt = 0;
  i__1 = dlsr01_1.ngc;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    jroot[i__] = 0;
  }
  hming = (abs(dls001_1.tn) + abs(dls001_1.h__)) *dls001_1.uround * 100.;

  switch (*job)
  {
    case 1:
      goto L100;
    case 2:
      goto L200;
    case 3:
      goto L300;
  }

  /* Evaluate g at initial T, and check for zero values. ------------------ */
  L100: dlsr01_1.t0 = dls001_1.tn;
  (*g)(&neq[1], &dlsr01_1.t0, &y[1], &dlsr01_1.ngc, &g0[1]);
  dlsr01_1.nge = 1;
  zroot = FALSE_;
  i__1 = dlsr01_1.ngc;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L110: */
    if ((d__1 = g0[i__], abs(d__1)) <= 0.)
    {
      zroot = TRUE_;
    }
  }
  if (!zroot)
  {
    goto L190;
  }
  /* g has a zero at T.  Look at g at T + (small increment). -------------- */
  /* Computing MAX */
  d__1 = hming / abs(dls001_1.h__);
  temp2 = max(d__1, .1);
  temp1 = temp2 * dls001_1.h__;
  dlsr01_1.t0 += temp1;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L120: */
    y[i__] += temp2 * yh[i__ + (yh_dim1 << 1)];
  }
  (*g)(&neq[1], &dlsr01_1.t0, &y[1], &dlsr01_1.ngc, &g0[1]);
  ++dlsr01_1.nge;
  zroot = FALSE_;
  i__1 = dlsr01_1.ngc;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L130: */
    if ((d__1 = g0[i__], abs(d__1)) <= 0.)
    {
      zroot = TRUE_;
    }
  }
  if (!zroot)
  {
    goto L190;
  }
  /* g has a zero at T and also close to T.  Take error return. ----------- */
  *irt =  - 1;
  return 0;

  L190: return 0;


  L200: if (dlsr01_1.irfnd == 0)
  {
    goto L260;
  }
  /* If a root was found on the previous step, evaluate G0 = g(T0). ------- */
  dintdy_(&dlsr01_1.t0, &c__0, &yh[yh_offset], nyh, &y[1], &iflag);
  (*g)(&neq[1], &dlsr01_1.t0, &y[1], &dlsr01_1.ngc, &g0[1]);
  ++dlsr01_1.nge;
  zroot = FALSE_;
  i__1 = dlsr01_1.ngc;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L210: */
    if ((d__1 = g0[i__], abs(d__1)) <= 0.)
    {
      zroot = TRUE_;
    }
  }
  if (!zroot)
  {
    goto L260;
  }
  /* g has a zero at T0.  Look at g at T + (small increment). ------------- */
  temp1 = d_sign(&hming, &dls001_1.h__);
  dlsr01_1.t0 += temp1;
  if ((dlsr01_1.t0 - dls001_1.tn) *dls001_1.h__ < 0.)
  {
    goto L230;
  }
  temp2 = temp1 / dls001_1.h__;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L220: */
    y[i__] += temp2 * yh[i__ + (yh_dim1 << 1)];
  }
  goto L240;
  L230: dintdy_(&dlsr01_1.t0, &c__0, &yh[yh_offset], nyh, &y[1], &iflag);
  L240: (*g)(&neq[1], &dlsr01_1.t0, &y[1], &dlsr01_1.ngc, &g0[1]);
  ++dlsr01_1.nge;
  zroot = FALSE_;
  i__1 = dlsr01_1.ngc;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    if ((d__1 = g0[i__], abs(d__1)) > 0.)
    {
      goto L250;
    }
    jroot[i__] = 1;
    zroot = TRUE_;
    L250: ;
  }
  if (!zroot)
  {
    goto L260;
  }
  /* g has a zero at T0 and also close to T0.  Return root. --------------- */
  *irt = 1;
  return 0;
  /* G0 has no zero components.  Proceed to check relevant interval. ------ */
  L260: if (dls001_1.tn == dlsr01_1.tlast)
  {
    goto L390;
  }

  L300: 
  /* Set T1 to TN or TOUTC, whichever comes first, and get g at T1. ------- */
  if (dlsr01_1.itaskc == 2 || dlsr01_1.itaskc == 3 || dlsr01_1.itaskc == 5)
  {
    goto L310;
  }
  if ((dlsr01_1.toutc - dls001_1.tn) *dls001_1.h__ >= 0.)
  {
    goto L310;
  }
  t1 = dlsr01_1.toutc;
  if ((t1 - dlsr01_1.t0) *dls001_1.h__ <= 0.)
  {
    goto L390;
  }
  dintdy_(&t1, &c__0, &yh[yh_offset], nyh, &y[1], &iflag);
  goto L330;
  L310: t1 = dls001_1.tn;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L320: */
    y[i__] = yh[i__ + yh_dim1];
  }
  L330: (*g)(&neq[1], &t1, &y[1], &dlsr01_1.ngc, &g1[1]);
  ++dlsr01_1.nge;
  /* Call DROOTS to search for root in interval from T0 to T1. ------------ */
  jflag = 0;
  L350: droots_(&dlsr01_1.ngc, &hming, &jflag, &dlsr01_1.t0, &t1, &g0[1],
    &g1[1],  &gx[1], &x, &jroot[1]);
  if (jflag > 1)
  {
    goto L360;
  }
  dintdy_(&x, &c__0, &yh[yh_offset], nyh, &y[1], &iflag);
  (*g)(&neq[1], &x, &y[1], &dlsr01_1.ngc, &gx[1]);
  ++dlsr01_1.nge;
  goto L350;
  L360: dlsr01_1.t0 = x;
  dcopy_(&dlsr01_1.ngc, &gx[1], &c__1, &g0[1], &c__1);
  if (jflag == 4)
  {
    goto L390;
  }
  /* Found a root.  Interpolate to X and return. -------------------------- */
  dintdy_(&x, &c__0, &yh[yh_offset], nyh, &y[1], &iflag);
  *irt = 1;
  return 0;

  L390: return 0;
  /* ----------------------- End of Subroutine DRCHEK ---------------------- */
} /* drchek_ */

/* DECK DROOTS */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::droots_
   (integer *ng, doublereal *hmin, integer *jflag, doublereal *x0, doublereal
   *x1, doublereal *g0, doublereal *g1, doublereal *gx, doublereal *x, integer
   *jroot)
{
  /* Initialized data */

  static doublereal zero = 0.;
  static doublereal half = .5;
  static doublereal tenth = .1;
  static doublereal five = 5.;

  /* System generated locals */
  integer i__1;
  doublereal d__1;

  /* Local variables */
  static integer i__;
  static doublereal t2, tmax;
  static logical xroot, zroot, sgnchg;
  static integer imxold, nxlast;
  static doublereal fracsub, fracint;

  /* ----------------------------------------------------------------------- */
  /* This subroutine finds the leftmost root of a set of arbitrary */
  /* functions gi(x) (i = 1,...,NG) in an interval (X0,X1).  Only roots */
  /* of odd multiplicity (i.e. changes of sign of the gi) are found. */
  /* Here the sign of X1 - X0 is arbitrary, but is constant for a given */
  /* problem, and -leftmost- means nearest to X0. */
  /* The values of the vector-valued function g(x) = (gi, i=1...NG) */
  /* are communicated through the call sequence of DROOTS. */
  /* The method used is the Illinois algorithm. */

  /* Reference: */
  /* Kathie L. Hiebert and Lawrence F. Shampine, Implicitly Defined */
  /* Output Points for Solutions of ODEs, Sandia Report SAND80-0180, */
  /* February 1980. */

  /* Description of parameters. */

  /* NG     = number of functions gi, or the number of components of */
  /*          the vector valued function g(x).  Input only. */

  /* HMIN   = resolution parameter in X.  Input only.  When a root is */
  /*          found, it is located only to within an error of HMIN in X. */
  /*          Typically, HMIN should be set to something on the order of */
  /*               100 * UROUND * MAX(ABS(X0),ABS(X1)), */
  /*          where UROUND is the unit roundoff of the machine. */

  /* JFLAG  = integer flag for input and output communication. */

  /*          On input, set JFLAG = 0 on the first call for the problem, */
  /*          and leave it unchanged until the problem is completed. */
  /*          (The problem is completed when JFLAG .ge. 2 on return.) */

  /*          On output, JFLAG has the following values and meanings: */
  /*          JFLAG = 1 means DROOTS needs a value of g(x).  Set GX = g(X) */
  /*                    and call DROOTS again. */
  /*          JFLAG = 2 means a root has been found.  The root is */
  /*                    at X, and GX contains g(X).  (Actually, X is the */
  /*                    rightmost approximation to the root on an interval */
  /*                    (X0,X1) of size HMIN or less.) */
  /*          JFLAG = 3 means X = X1 is a root, with one or more of the gi */
  /*                    being zero at X1 and no sign changes in (X0,X1). */
  /*                    GX contains g(X) on output. */
  /*          JFLAG = 4 means no roots (of odd multiplicity) were */
  /*                    found in (X0,X1) (no sign changes). */

  /* X0,X1  = endpoints of the interval where roots are sought. */
  /*          X1 and X0 are input when JFLAG = 0 (first call), and */
  /*          must be left unchanged between calls until the problem is */
  /*          completed.  X0 and X1 must be distinct, but X1 - X0 may be */
  /*          of either sign.  However, the notion of -left- and -right- */
  /*          will be used to mean nearer to X0 or X1, respectively. */
  /*          When JFLAG .ge. 2 on return, X0 and X1 are output, and */
  /*          are the endpoints of the relevant interval. */

  /* G0,G1  = arrays of length NG containing the vectors g(X0) and g(X1), */
  /*          respectively.  When JFLAG = 0, G0 and G1 are input and */
  /*          none of the G0(i) should be zero. */
  /*          When JFLAG .ge. 2 on return, G0 and G1 are output. */

  /* GX     = array of length NG containing g(X).  GX is input */
  /*          when JFLAG = 1, and output when JFLAG .ge. 2. */

  /* X      = independent variable value.  Output only. */
  /*          When JFLAG = 1 on output, X is the point at which g(x) */
  /*          is to be evaluated and loaded into GX. */
  /*          When JFLAG = 2 or 3, X is the root. */
  /*          When JFLAG = 4, X is the right endpoint of the interval, X1. */

  /* JROOT  = integer array of length NG.  Output only. */
  /*          When JFLAG = 2 or 3, JROOT indicates which components */
  /*          of g(x) have a root at X.  JROOT(i) is 1 if the i-th */
  /*          component has a root, and JROOT(i) = 0 otherwise. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --jroot;
  --gx;
  --g1;
  --g0;

  /* Function Body */

  if (*jflag == 1)
  {
    goto L200;
  }
  /* JFLAG .ne. 1.  Check for change in sign of g or zero at X1. ---------- */
  dlsr01_2.imax = 0;
  tmax = zero;
  zroot = FALSE_;
  i__1 =  *ng;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    if ((d__1 = g1[i__], abs(d__1)) > zero)
    {
      goto L110;
    }
    zroot = TRUE_;
    goto L120;
    /* At this point, G0(i) has been checked and cannot be zero. ------------ */
    L110: if (d_sign(&c_b3, &g0[i__]) == d_sign(&c_b3, &g1[i__]))
    {
      goto L120;
    }
    t2 = (d__1 = g1[i__] / (g1[i__] - g0[i__]), abs(d__1));
    if (t2 <= tmax)
    {
      goto L120;
    }
    tmax = t2;
    dlsr01_2.imax = i__;
    L120: ;
  }
  if (dlsr01_2.imax > 0)
  {
    goto L130;
  }
  sgnchg = FALSE_;
  goto L140;
  L130: sgnchg = TRUE_;
  L140: if (!sgnchg)
  {
    goto L400;
  }
  /* There is a sign change.  Find the first root in the interval. -------- */
  xroot = FALSE_;
  nxlast = 0;
  dlsr01_2.last = 1;

  /* Repeat until the first root in the interval is found.  Loop point. --- */
  L150: if (xroot)
  {
    goto L300;
  }
  if (nxlast == dlsr01_2.last)
  {
    goto L160;
  }
  dlsr01_2.alpha = 1.;
  goto L180;
  L160: if (dlsr01_2.last == 0)
  {
    goto L170;
  }
  dlsr01_2.alpha *= .5;
  goto L180;
  L170: dlsr01_2.alpha *= 2.;
  L180: dlsr01_2.x2 =  *x1 - (*x1 -  *x0) *g1[dlsr01_2.imax] / 
    (g1[dlsr01_2.imax] - dlsr01_2.alpha * g0[dlsr01_2.imax]);
  /* If X2 is too close to X0 or X1, adjust it inward, by a fractional ---- */
  /* distance that is between 0.1 and 0.5. -------------------------------- */
  if ((d__1 = dlsr01_2.x2 -  *x0, abs(d__1)) < half **hmin)
  {
    fracint = (d__1 =  *x1 -  *x0, abs(d__1)) /  *hmin;
    fracsub = tenth;
    if (fracint <= five)
    {
      fracsub = half / fracint;
    }
    dlsr01_2.x2 =  *x0 + fracsub *(*x1 -  *x0);
  }
  if ((d__1 =  *x1 - dlsr01_2.x2, abs(d__1)) < half **hmin)
  {
    fracint = (d__1 =  *x1 -  *x0, abs(d__1)) /  *hmin;
    fracsub = tenth;
    if (fracint <= five)
    {
      fracsub = half / fracint;
    }
    dlsr01_2.x2 =  *x1 - fracsub *(*x1 -  *x0);
  }
  *jflag = 1;
  *x = dlsr01_2.x2;
  /* Return to the calling routine to get a value of GX = g(X). ----------- */
  return 0;
  /* Check to see in which interval g changes sign. ----------------------- */
  L200: imxold = dlsr01_2.imax;
  dlsr01_2.imax = 0;
  tmax = zero;
  zroot = FALSE_;
  i__1 =  *ng;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    if ((d__1 = gx[i__], abs(d__1)) > zero)
    {
      goto L210;
    }
    zroot = TRUE_;
    goto L220;
    /* Neither G0(i) nor GX(i) can be zero at this point. ------------------- */
    L210: if (d_sign(&c_b3, &g0[i__]) == d_sign(&c_b3, &gx[i__]))
    {
      goto L220;
    }
    t2 = (d__1 = gx[i__] / (gx[i__] - g0[i__]), abs(d__1));
    if (t2 <= tmax)
    {
      goto L220;
    }
    tmax = t2;
    dlsr01_2.imax = i__;
    L220: ;
  }
  if (dlsr01_2.imax > 0)
  {
    goto L230;
  }
  sgnchg = FALSE_;
  dlsr01_2.imax = imxold;
  goto L240;
  L230: sgnchg = TRUE_;
  L240: nxlast = dlsr01_2.last;
  if (!sgnchg)
  {
    goto L250;
  }
  /* Sign change between X0 and X2, so replace X1 with X2. ---------------- */
  *x1 = dlsr01_2.x2;
  dcopy_(ng, &gx[1], &c__1, &g1[1], &c__1);
  dlsr01_2.last = 1;
  xroot = FALSE_;
  goto L270;
  L250: if (!zroot)
  {
    goto L260;
  }
  /* Zero value at X2 and no sign change in (X0,X2), so X2 is a root. ----- */
  *x1 = dlsr01_2.x2;
  dcopy_(ng, &gx[1], &c__1, &g1[1], &c__1);
  xroot = TRUE_;
  goto L270;
  /* No sign change between X0 and X2.  Replace X0 with X2. --------------- */
  L260: dcopy_(ng, &gx[1], &c__1, &g0[1], &c__1);
  *x0 = dlsr01_2.x2;
  dlsr01_2.last = 0;
  xroot = FALSE_;
  L270: if ((d__1 =  *x1 -  *x0, abs(d__1)) <=  *hmin)
  {
    xroot = TRUE_;
  }
  goto L150;

  /* Return with X1 as the root.  Set JROOT.  Set X = X1 and GX = G1. ----- */
  L300: *jflag = 2;
  *x =  *x1;
  dcopy_(ng, &g1[1], &c__1, &gx[1], &c__1);
  i__1 =  *ng;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    jroot[i__] = 0;
    if ((d__1 = g1[i__], abs(d__1)) > zero)
    {
      goto L310;
    }
    jroot[i__] = 1;
    goto L320;
    L310: if (d_sign(&c_b3, &g0[i__]) != d_sign(&c_b3, &g1[i__]))
    {
      jroot[i__] = 1;
    }
    L320: ;
  }
  return 0;

  /* No sign change in the interval.  Check for zero at right endpoint. --- */
  L400: if (!zroot)
  {
    goto L420;
  }

  /* Zero value at X1 and no sign change in (X0,X1).  Return JFLAG = 3. --- */
  *x =  *x1;
  dcopy_(ng, &g1[1], &c__1, &gx[1], &c__1);
  i__1 =  *ng;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    jroot[i__] = 0;
    if ((d__1 = g1[i__], abs(d__1)) <= zero)
    {
      jroot[i__] = 1;
    }
    /* L410: */
  }
  *jflag = 3;
  return 0;

  /* No sign changes in this interval.  Set X = X1, return JFLAG = 4. ----- */
  L420: dcopy_(ng, &g1[1], &c__1, &gx[1], &c__1);
  *x =  *x1;
  *jflag = 4;
  return 0;
  /* ----------------------- End of Subroutine DROOTS ---------------------- */
} /* droots_ */

/* DECK DSRCAR */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dsrcar_
   (doublereal *rsav, integer *isav, integer *job)
{
  /* Initialized data */

  static integer lenrls = 218;
  static integer lenils = 37;
  static integer lenrla = 22;
  static integer lenila = 9;
  static integer lenrlr = 5;
  static integer lenilr = 9;

  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__, ioff;

  /* ----------------------------------------------------------------------- */
  /* This routine saves or restores (depending on JOB) the contents of */
  /* the Common blocks DLS001, DLSA01, DLSR01, which are used */
  /* internally by one or more ODEPACK solvers. */

  /* RSAV = real array of length 245 or more. */
  /* ISAV = integer array of length 55 or more. */
  /* JOB  = flag indicating to save or restore the Common blocks: */
  /*        JOB  = 1 if Common is to be saved (written to RSAV/ISAV) */
  /*        JOB  = 2 if Common is to be restored (read from RSAV/ISAV) */
  /*        A call with JOB = 2 presumes a prior call with JOB = 1. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --isav;
  --rsav;

  /* Function Body */

  if (*job == 2)
  {
    goto L100;
  }
  i__1 = lenrls;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    rsav[i__] = dls001_2.rls[i__ - 1];
  }
  i__1 = lenrla;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L15: */
    rsav[lenrls + i__] = dlsa01_3.rlsa[i__ - 1];
  }
  ioff = lenrls + lenrla;
  i__1 = lenrlr;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L20: */
    rsav[ioff + i__] = dlsr01_3.rlsr[i__ - 1];
  }

  i__1 = lenils;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L30: */
    isav[i__] = dls001_2.ils[i__ - 1];
  }
  i__1 = lenila;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L35: */
    isav[lenils + i__] = dlsa01_3.ilsa[i__ - 1];
  }
  ioff = lenils + lenila;
  i__1 = lenilr;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L40: */
    isav[ioff + i__] = dlsr01_3.ilsr[i__ - 1];
  }

  return 0;

  L100: i__1 = lenrls;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L110: */
    dls001_2.rls[i__ - 1] = rsav[i__];
  }
  i__1 = lenrla;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L115: */
    dlsa01_3.rlsa[i__ - 1] = rsav[lenrls + i__];
  }
  ioff = lenrls + lenrla;
  i__1 = lenrlr;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L120: */
    dlsr01_3.rlsr[i__ - 1] = rsav[ioff + i__];
  }

  i__1 = lenils;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L130: */
    dls001_2.ils[i__ - 1] = isav[i__];
  }
  i__1 = lenila;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L135: */
    dlsa01_3.ilsa[i__ - 1] = isav[lenils + i__];
  }
  ioff = lenils + lenila;
  i__1 = lenilr;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L140: */
    dlsr01_3.ilsr[i__ - 1] = isav[ioff + i__];
  }

  return 0;
  /* ----------------------- End of Subroutine DSRCAR ---------------------- */
} /* dsrcar_ */

/* DECK DSTODPK */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider
   ::dstodpk_(integer *neq, doublereal *y, doublereal *yh, integer *nyh,
   doublereal *yh1, doublereal *ewt, doublereal *savf, doublereal *savx,
   doublereal *acor, doublereal *wm, integer *iwm, S_fp f, U_fp jac, U_fp psol)
{
  /* System generated locals */
  integer yh_dim1, yh_offset, i__1, i__2;
  doublereal d__1, d__2, d__3;

  /* Local variables */
  static integer i__, j, m;
  static doublereal r__;
  static integer i1, jb;
  static doublereal rh, del, ddn;
  static integer ncf;
  static doublereal dsm, dup, dcon, delp, rhdn, exdn;
  static integer iret;
  static doublereal rhsm;
  static integer newq;
  static doublereal exsm, rhup, exup, told;
  static integer iredo;

  /* ----------------------------------------------------------------------- */
  /* DSTODPK performs one step of the integration of an initial value */
  /* problem for a system of Ordinary Differential Equations. */
  /* ----------------------------------------------------------------------- */
  /* The following changes were made to generate Subroutine DSTODPK */
  /* from Subroutine DSTODE: */
  /* 1. The array SAVX was added to the call sequence. */
  /* 2. PJAC and SLVS were replaced by PSOL in the call sequence. */
  /* 3. The Common block /DLPK01/ was added for communication. */
  /* 4. The test constant EPCON is loaded into Common below statement */
  /*    numbers 125 and 155, and used below statement 400. */
  /* 5. The Newton iteration counter MNEWT is set below 220 and 400. */
  /* 6. The call to PJAC was replaced with a call to DPKSET (fixed name), */
  /*    with a longer call sequence, called depending on JACFLG. */
  /* 7. The corrector residual is stored in SAVX (not Y) at 360, */
  /*    and the solution vector is in SAVX in the 380 loop. */
  /* 8. SLVS was renamed DSOLPK and includes NEQ, SAVX, EWT, F, and JAC. */
  /*    SAVX was added because DSOLPK now needs Y and SAVF undisturbed. */
  /* 9. The nonlinear convergence failure count NCFN is set at 430. */
  /* ----------------------------------------------------------------------- */
  /* Note: DSTODPK is independent of the value of the iteration method */
  /* indicator MITER, when this is .ne. 0, and hence is independent */
  /* of the type of chord method used, or the Jacobian structure. */
  /* Communication with DSTODPK is done with the following variables: */

  /* NEQ    = integer array containing problem size in NEQ(1), and */
  /*          passed as the NEQ argument in all calls to F and JAC. */
  /* Y      = an array of length .ge. N used as the Y argument in */
  /*          all calls to F and JAC. */
  /* YH     = an NYH by LMAX array containing the dependent variables */
  /*          and their approximate scaled derivatives, where */
  /*          LMAX = MAXORD + 1.  YH(i,j+1) contains the approximate */
  /*          j-th derivative of y(i), scaled by H**j/factorial(j) */
  /*          (j = 0,1,...,NQ).  On entry for the first step, the first */
  /*          two columns of YH must be set from the initial values. */
  /* NYH    = a constant integer .ge. N, the first dimension of YH. */
  /* YH1    = a one-dimensional array occupying the same space as YH. */
  /* EWT    = an array of length N containing multiplicative weights */
  /*          for local error measurements.  Local errors in y(i) are */
  /*          compared to 1.0/EWT(i) in various error tests. */
  /* SAVF   = an array of working storage, of length N. */
  /*          Also used for input of YH(*,MAXORD+2) when JSTART = -1 */
  /*          and MAXORD .lt. the current order NQ. */
  /* SAVX   = an array of working storage, of length N. */
  /* ACOR   = a work array of length N, used for the accumulated */
  /*          corrections.  On a successful return, ACOR(i) contains */
  /*          the estimated one-step local error in y(i). */
  /* WM,IWM = real and integer work arrays associated with matrix */
  /*          operations in chord iteration (MITER .ne. 0). */
  /* CCMAX  = maximum relative change in H*EL0 before DPKSET is called. */
  /* H      = the step size to be attempted on the next step. */
  /*          H is altered by the error control algorithm during the */
  /*          problem.  H can be either positive or negative, but its */
  /*          sign must remain constant throughout the problem. */
  /* HMIN   = the minimum absolute value of the step size H to be used. */
  /* HMXI   = inverse of the maximum absolute value of H to be used. */
  /*          HMXI = 0.0 is allowed and corresponds to an infinite HMAX. */
  /*          HMIN and HMXI may be changed at any time, but will not */
  /*          take effect until the next change of H is considered. */
  /* TN     = the independent variable. TN is updated on each step taken. */
  /* JSTART = an integer used for input only, with the following */
  /*          values and meanings: */
  /*               0  perform the first step. */
  /*           .gt.0  take a new step continuing from the last. */
  /*              -1  take the next step with a new value of H, MAXORD, */
  /*                    N, METH, MITER, and/or matrix parameters. */
  /*              -2  take the next step with a new value of H, */
  /*                    but with other inputs unchanged. */
  /*          On return, JSTART is set to 1 to facilitate continuation. */
  /* KFLAG  = a completion code with the following meanings: */
  /*               0  the step was succesful. */
  /*              -1  the requested error could not be achieved. */
  /*              -2  corrector convergence could not be achieved. */
  /*              -3  fatal error in DPKSET or DSOLPK. */
  /*          A return with KFLAG = -1 or -2 means either */
  /*          ABS(H) = HMIN or 10 consecutive failures occurred. */
  /*          On a return with KFLAG negative, the values of TN and */
  /*          the YH array are as of the beginning of the last */
  /*          step, and H is the last step size attempted. */
  /* MAXORD = the maximum order of integration method to be allowed. */
  /* MAXCOR = the maximum number of corrector iterations allowed. */
  /* MSBP   = maximum number of steps between DPKSET calls (MITER .gt. 0). */
  /* MXNCF  = maximum number of convergence failures allowed. */
  /* METH/MITER = the method flags.  See description in driver. */
  /* N      = the number of first-order differential equations. */
  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --neq;
  --y;
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --yh1;
  --ewt;
  --savf;
  --savx;
  --acor;
  --wm;
  --iwm;

  /* Function Body */
  dls001_3.kflag = 0;
  told = dls001_3.tn;
  ncf = 0;
  dls001_3.ierpj = 0;
  dls001_3.iersl = 0;
  dls001_3.jcur = 0;
  dls001_3.icf = 0;
  delp = 0.;
  if (dls001_3.jstart > 0)
  {
    goto L200;
  }
  if (dls001_3.jstart ==  - 1)
  {
    goto L100;
  }
  if (dls001_3.jstart ==  - 2)
  {
    goto L160;
  }
  /* ----------------------------------------------------------------------- */
  /* On the first call, the order is set to 1, and other variables are */
  /* initialized.  RMAX is the maximum ratio by which H can be increased */
  /* in a single step.  It is initially 1.E4 to compensate for the small */
  /* initial H, but then is normally equal to 10.  If a failure */
  /* occurs (in corrector convergence or error test), RMAX is set at 2 */
  /* for the next increase. */
  /* ----------------------------------------------------------------------- */
  dls001_3.lmax = dls001_3.maxord + 1;
  dls001_3.nq = 1;
  dls001_3.l = 2;
  dls001_3.ialth = 2;
  dls001_3.rmax = 1e4;
  dls001_3.rc = 0.;
  dls001_3.el0 = 1.;
  dls001_3.crate = .7;
  dls001_3.hold = dls001_3.h__;
  dls001_3.meo = dls001_3.meth;
  dls001_3.nslp = 0;
  dls001_3.ipup = dls001_3.miter;
  iret = 3;
  goto L140;
  /* ----------------------------------------------------------------------- */
  /* The following block handles preliminaries needed when JSTART = -1. */
  /* IPUP is set to MITER to force a matrix update. */
  /* If an order increase is about to be considered (IALTH = 1), */
  /* IALTH is reset to 2 to postpone consideration one more step. */
  /* If the caller has changed METH, DCFODE is called to reset */
  /* the coefficients of the method. */
  /* If the caller has changed MAXORD to a value less than the current */
  /* order NQ, NQ is reduced to MAXORD, and a new H chosen accordingly. */
  /* If H is to be changed, YH must be rescaled. */
  /* If H or METH is being changed, IALTH is reset to L = NQ + 1 */
  /* to prevent further changes in H for that many steps. */
  /* ----------------------------------------------------------------------- */
  L100: dls001_3.ipup = dls001_3.miter;
  dls001_3.lmax = dls001_3.maxord + 1;
  if (dls001_3.ialth == 1)
  {
    dls001_3.ialth = 2;
  }
  if (dls001_3.meth == dls001_3.meo)
  {
    goto L110;
  }
  dcfode_(&dls001_3.meth, dls001_3.elco, dls001_3.tesco);
  dls001_3.meo = dls001_3.meth;
  if (dls001_3.nq > dls001_3.maxord)
  {
    goto L120;
  }
  dls001_3.ialth = dls001_3.l;
  iret = 1;
  goto L150;
  L110: if (dls001_3.nq <= dls001_3.maxord)
  {
    goto L160;
  }
  L120: dls001_3.nq = dls001_3.maxord;
  dls001_3.l = dls001_3.lmax;
  i__1 = dls001_3.l;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L125: */
    dls001_3.el[i__ - 1] = dls001_3.elco[i__ + dls001_3.nq * 13-14];
  }
  dls001_3.nqnyh = dls001_3.nq **nyh;
  dls001_3.rc = dls001_3.rc * dls001_3.el[0] / dls001_3.el0;
  dls001_3.el0 = dls001_3.el[0];
  dls001_3.conit = .5 / (dls001_3.nq + 2);
  dlpk01_1.epcon = dls001_3.conit * dls001_3.tesco[dls001_3.nq *3-2];
  ddn = dvnorm_(&dls001_3.n, &savf[1], &ewt[1]) / dls001_3.tesco[dls001_3.l*3
    -3];
  exdn = 1. / dls001_3.l;
  rhdn = 1. / (pow_dd(&ddn, &exdn) *1.3 + 1.3e-6);
  rh = min(rhdn, 1.);
  iredo = 3;
  if (dls001_3.h__ == dls001_3.hold)
  {
    goto L170;
  }
  /* Computing MIN */
  d__2 = rh, d__3 = (d__1 = dls001_3.h__ / dls001_3.hold, abs(d__1));
  rh = min(d__2, d__3);
  dls001_3.h__ = dls001_3.hold;
  goto L175;
  /* ----------------------------------------------------------------------- */
  /* DCFODE is called to get all the integration coefficients for the */
  /* current METH.  Then the EL vector and related constants are reset */
  /* whenever the order NQ is changed, or at the start of the problem. */
  /* ----------------------------------------------------------------------- */
  L140: dcfode_(&dls001_3.meth, dls001_3.elco, dls001_3.tesco);
  L150: i__1 = dls001_3.l;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L155: */
    dls001_3.el[i__ - 1] = dls001_3.elco[i__ + dls001_3.nq * 13-14];
  }
  dls001_3.nqnyh = dls001_3.nq **nyh;
  dls001_3.rc = dls001_3.rc * dls001_3.el[0] / dls001_3.el0;
  dls001_3.el0 = dls001_3.el[0];
  dls001_3.conit = .5 / (dls001_3.nq + 2);
  dlpk01_1.epcon = dls001_3.conit * dls001_3.tesco[dls001_3.nq *3-2];
  switch (iret)
  {
    case 1:
      goto L160;
    case 2:
      goto L170;
    case 3:
      goto L200;
  }
  /* ----------------------------------------------------------------------- */
  /* If H is being changed, the H ratio RH is checked against */
  /* RMAX, HMIN, and HMXI, and the YH array rescaled.  IALTH is set to */
  /* L = NQ + 1 to prevent a change of H for that many steps, unless */
  /* forced by a convergence or error test failure. */
  /* ----------------------------------------------------------------------- */
  L160: if (dls001_3.h__ == dls001_3.hold)
  {
    goto L200;
  }
  rh = dls001_3.h__ / dls001_3.hold;
  dls001_3.h__ = dls001_3.hold;
  iredo = 3;
  goto L175;
  L170: 
  /* Computing MAX */
  d__1 = rh, d__2 = dls001_3.hmin / abs(dls001_3.h__);
  rh = max(d__1, d__2);
  L175: rh = min(rh, dls001_3.rmax);
  /* Computing MAX */
  d__1 = 1., d__2 = abs(dls001_3.h__) *dls001_3.hmxi * rh;
  rh /= max(d__1, d__2);
  r__ = 1.;
  i__1 = dls001_3.l;
  for (j = 2; j <= i__1; ++j)
  {
    r__ *= rh;
    i__2 = dls001_3.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L180: */
      yh[i__ + j * yh_dim1] *= r__;
    }
  }
  dls001_3.h__ *= rh;
  dls001_3.rc *= rh;
  dls001_3.ialth = dls001_3.l;
  if (iredo == 0)
  {
    goto L690;
  }
  /* ----------------------------------------------------------------------- */
  /* This section computes the predicted values by effectively */
  /* multiplying the YH array by the Pascal triangle matrix. */
  /* The flag IPUP is set according to whether matrix data is involved */
  /* (JACFLG .ne. 0) or not (JACFLG = 0), to trigger a call to DPKSET. */
  /* IPUP is set to MITER when RC differs from 1 by more than CCMAX, */
  /* and at least every MSBP steps, when JACFLG = 1. */
  /* RC is the ratio of new to old values of the coefficient  H*EL(1). */
  /* ----------------------------------------------------------------------- */
  L200: if (dlpk01_1.jacflg != 0)
  {
    goto L202;
  }
  dls001_3.ipup = 0;
  dls001_3.crate = .7;
  goto L205;
  L202: if ((d__1 = dls001_3.rc - 1., abs(d__1)) > dls001_3.ccmax)
  {
    dls001_3.ipup = dls001_3.miter;
  }
  if (dls001_3.nst >= dls001_3.nslp + dls001_3.msbp)
  {
    dls001_3.ipup = dls001_3.miter;
  }
  L205: dls001_3.tn += dls001_3.h__;
  i1 = dls001_3.nqnyh + 1;
  i__2 = dls001_3.nq;
  for (jb = 1; jb <= i__2; ++jb)
  {
    i1 -=  *nyh;
    /* DIR$ IVDEP */
    i__1 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__1; ++i__)
    {
      /* L210: */
      yh1[i__] += yh1[i__ +  *nyh];
    }
    /* L215: */
  }
  /* ----------------------------------------------------------------------- */
  /* Up to MAXCOR corrector iterations are taken.  A convergence test is */
  /* made on the RMS-norm of each correction, weighted by the error */
  /* weight vector EWT.  The sum of the corrections is accumulated in the */
  /* vector ACOR(i).  The YH array is not altered in the corrector loop. */
  /* ----------------------------------------------------------------------- */
  L220: m = 0;
  dlpk01_1.mnewt = 0;
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L230: */
    y[i__] = yh[i__ + yh_dim1];
  }
  (*f)(&neq[1], &dls001_3.tn, &y[1], &savf[1]);
  ++dls001_3.nfe;
  if (dls001_3.ipup <= 0)
  {
    goto L250;
  }
  /* ----------------------------------------------------------------------- */
  /* If indicated, DPKSET is called to update any matrix data needed, */
  /* before starting the corrector iteration. */
  /* IPUP is set to 0 as an indicator that this has been done. */
  /* ----------------------------------------------------------------------- */
  dpkset_(&neq[1], &y[1], &yh1[1], &ewt[1], &acor[1], &savf[1], &wm[1], &iwm[1],
    (S_fp)f, (U_fp)jac);
  dls001_3.ipup = 0;
  dls001_3.rc = 1.;
  dls001_3.nslp = dls001_3.nst;
  dls001_3.crate = .7;
  if (dls001_3.ierpj != 0)
  {
    goto L430;
  }
  L250: i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L260: */
    acor[i__] = 0.;
  }
  L270: if (dls001_3.miter != 0)
  {
    goto L350;
  }
  /* ----------------------------------------------------------------------- */
  /* In the case of functional iteration, update Y directly from */
  /* the result of the last function evaluation. */
  /* ----------------------------------------------------------------------- */
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    savf[i__] = dls001_3.h__ * savf[i__] - yh[i__ + (yh_dim1 << 1)];
    /* L290: */
    y[i__] = savf[i__] - acor[i__];
  }
  del = dvnorm_(&dls001_3.n, &y[1], &ewt[1]);
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    y[i__] = yh[i__ + yh_dim1] + dls001_3.el[0] *savf[i__];
    /* L300: */
    acor[i__] = savf[i__];
  }
  goto L400;
  /* ----------------------------------------------------------------------- */
  /* In the case of the chord method, compute the corrector error, */
  /* and solve the linear system with that as right-hand side and */
  /* P as coefficient matrix. */
  /* ----------------------------------------------------------------------- */
  L350: i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L360: */
    savx[i__] = dls001_3.h__ * savf[i__] - (yh[i__ + (yh_dim1 << 1)] +
      acor[i__]);
  }
  dsolpk_(&neq[1], &y[1], &savf[1], &savx[1], &ewt[1], &wm[1], &iwm[1], (S_fp)f,
    (U_fp)psol);
  if (dls001_3.iersl < 0)
  {
    goto L430;
  }
  if (dls001_3.iersl > 0)
  {
    goto L410;
  }
  del = dvnorm_(&dls001_3.n, &savx[1], &ewt[1]);
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    acor[i__] += savx[i__];
    /* L380: */
    y[i__] = yh[i__ + yh_dim1] + dls001_3.el[0] *acor[i__];
  }
  /* ----------------------------------------------------------------------- */
  /* Test for convergence.  If M .gt. 0, an estimate of the convergence */
  /* rate constant is stored in CRATE, and this is used in the test. */
  /* ----------------------------------------------------------------------- */
  L400: if (m != 0)
  {
    /* Computing MAX */
    d__1 = dls001_3.crate * .2, d__2 = del / delp;
    dls001_3.crate = max(d__1, d__2);
  }
  /* Computing MIN */
  d__1 = 1., d__2 = dls001_3.crate * 1.5;
  dcon = del * min(d__1, d__2) / dlpk01_1.epcon;
  if (dcon <= 1.)
  {
    goto L450;
  }
  ++m;
  if (m == dls001_3.maxcor)
  {
    goto L410;
  }
  if (m >= 2 && del > delp *2.)
  {
    goto L410;
  }
  dlpk01_1.mnewt = m;
  delp = del;
  (*f)(&neq[1], &dls001_3.tn, &y[1], &savf[1]);
  ++dls001_3.nfe;
  goto L270;
  /* ----------------------------------------------------------------------- */
  /* The corrector iteration failed to converge. */
  /* If MITER .ne. 0 and the Jacobian is out of date, DPKSET is called for */
  /* the next try.  Otherwise the YH array is retracted to its values */
  /* before prediction, and H is reduced, if possible.  If H cannot be */
  /* reduced or MXNCF failures have occurred, exit with KFLAG = -2. */
  /* ----------------------------------------------------------------------- */
  L410: if (dls001_3.miter == 0 || dls001_3.jcur == 1 || dlpk01_1.jacflg == 0)
  {
    goto L430;
  }
  dls001_3.icf = 1;
  dls001_3.ipup = dls001_3.miter;
  goto L220;
  L430: dls001_3.icf = 2;
  ++ncf;
  ++dlpk01_1.ncfn;
  dls001_3.rmax = 2.;
  dls001_3.tn = told;
  i1 = dls001_3.nqnyh + 1;
  i__2 = dls001_3.nq;
  for (jb = 1; jb <= i__2; ++jb)
  {
    i1 -=  *nyh;
    /* DIR$ IVDEP */
    i__1 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__1; ++i__)
    {
      /* L440: */
      yh1[i__] -= yh1[i__ +  *nyh];
    }
    /* L445: */
  }
  if (dls001_3.ierpj < 0 || dls001_3.iersl < 0)
  {
    goto L680;
  }
  if (abs(dls001_3.h__) <= dls001_3.hmin *1.00001)
  {
    goto L670;
  }
  if (ncf == dls001_3.mxncf)
  {
    goto L670;
  }
  rh = .5;
  dls001_3.ipup = dls001_3.miter;
  iredo = 1;
  goto L170;
  /* ----------------------------------------------------------------------- */
  /* The corrector has converged.  JCUR is set to 0 */
  /* to signal that the Jacobian involved may need updating later. */
  /* The local error test is made and control passes to statement 500 */
  /* if it fails. */
  /* ----------------------------------------------------------------------- */
  L450: dls001_3.jcur = 0;
  if (m == 0)
  {
    dsm = del / dls001_3.tesco[dls001_3.nq *3-2];
  }
  if (m > 0)
  {
    dsm = dvnorm_(&dls001_3.n, &acor[1], &ewt[1]) / dls001_3.tesco[dls001_3.nq
      *3-2];
  }
  if (dsm > 1.)
  {
    goto L500;
  }
  /* ----------------------------------------------------------------------- */
  /* After a successful step, update the YH array. */
  /* Consider changing H if IALTH = 1.  Otherwise decrease IALTH by 1. */
  /* If IALTH is then 1 and NQ .lt. MAXORD, then ACOR is saved for */
  /* use in a possible order increase on the next step. */
  /* If a change in H is considered, an increase or decrease in order */
  /* by one is considered also.  A change in H is made only if it is by a */
  /* factor of at least 1.1.  If not, IALTH is set to 3 to prevent */
  /* testing for that many steps. */
  /* ----------------------------------------------------------------------- */
  dls001_3.kflag = 0;
  iredo = 0;
  ++dls001_3.nst;
  dls001_3.hu = dls001_3.h__;
  dls001_3.nqu = dls001_3.nq;
  i__2 = dls001_3.l;
  for (j = 1; j <= i__2; ++j)
  {
    i__1 = dls001_3.n;
    for (i__ = 1; i__ <= i__1; ++i__)
    {
      /* L470: */
      yh[i__ + j * yh_dim1] += dls001_3.el[j - 1] *acor[i__];
    }
  }
  --dls001_3.ialth;
  if (dls001_3.ialth == 0)
  {
    goto L520;
  }
  if (dls001_3.ialth > 1)
  {
    goto L700;
  }
  if (dls001_3.l == dls001_3.lmax)
  {
    goto L700;
  }
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L490: */
    yh[i__ + dls001_3.lmax * yh_dim1] = acor[i__];
  }
  goto L700;
  /* ----------------------------------------------------------------------- */
  /* The error test failed.  KFLAG keeps track of multiple failures. */
  /* Restore TN and the YH array to their previous values, and prepare */
  /* to try the step again.  Compute the optimum step size for this or */
  /* one lower order.  After 2 or more failures, H is forced to decrease */
  /* by a factor of 0.2 or less. */
  /* ----------------------------------------------------------------------- */
  L500: --dls001_3.kflag;
  dls001_3.tn = told;
  i1 = dls001_3.nqnyh + 1;
  i__1 = dls001_3.nq;
  for (jb = 1; jb <= i__1; ++jb)
  {
    i1 -=  *nyh;
    /* DIR$ IVDEP */
    i__2 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__2; ++i__)
    {
      /* L510: */
      yh1[i__] -= yh1[i__ +  *nyh];
    }
    /* L515: */
  }
  dls001_3.rmax = 2.;
  if (abs(dls001_3.h__) <= dls001_3.hmin *1.00001)
  {
    goto L660;
  }
  if (dls001_3.kflag <=  - 3)
  {
    goto L640;
  }
  iredo = 2;
  rhup = 0.;
  goto L540;
  /* ----------------------------------------------------------------------- */
  /* Regardless of the success or failure of the step, factors */
  /* RHDN, RHSM, and RHUP are computed, by which H could be multiplied */
  /* at order NQ - 1, order NQ, or order NQ + 1, respectively. */
  /* In the case of failure, RHUP = 0.0 to avoid an order increase. */
  /* the largest of these is determined and the new order chosen */
  /* accordingly.  If the order is to be increased, we compute one */
  /* additional scaled derivative. */
  /* ----------------------------------------------------------------------- */
  L520: rhup = 0.;
  if (dls001_3.l == dls001_3.lmax)
  {
    goto L540;
  }
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L530: */
    savf[i__] = acor[i__] - yh[i__ + dls001_3.lmax * yh_dim1];
  }
  dup = dvnorm_(&dls001_3.n, &savf[1], &ewt[1]) / dls001_3.tesco[dls001_3.nq *3
    -1];
  exup = 1. / (dls001_3.l + 1);
  rhup = 1. / (pow_dd(&dup, &exup) *1.4 + 1.4e-6);
  L540: exsm = 1. / dls001_3.l;
  rhsm = 1. / (pow_dd(&dsm, &exsm) *1.2 + 1.2e-6);
  rhdn = 0.;
  if (dls001_3.nq == 1)
  {
    goto L560;
  }
  ddn = dvnorm_(&dls001_3.n, &yh[dls001_3.l *yh_dim1 + 1], &ewt[1]) /
    dls001_3.tesco[dls001_3.nq *3-3];
  exdn = 1. / dls001_3.nq;
  rhdn = 1. / (pow_dd(&ddn, &exdn) *1.3 + 1.3e-6);
  L560: if (rhsm >= rhup)
  {
    goto L570;
  }
  if (rhup > rhdn)
  {
    goto L590;
  }
  goto L580;
  L570: if (rhsm < rhdn)
  {
    goto L580;
  }
  newq = dls001_3.nq;
  rh = rhsm;
  goto L620;
  L580: newq = dls001_3.nq - 1;
  rh = rhdn;
  if (dls001_3.kflag < 0 && rh > 1.)
  {
    rh = 1.;
  }
  goto L620;
  L590: newq = dls001_3.l;
  rh = rhup;
  if (rh < 1.1)
  {
    goto L610;
  }
  r__ = dls001_3.el[dls001_3.l - 1] / dls001_3.l;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L600: */
    yh[i__ + (newq + 1) *yh_dim1] = acor[i__] *r__;
  }
  goto L630;
  L610: dls001_3.ialth = 3;
  goto L700;
  L620: if (dls001_3.kflag == 0 && rh < 1.1)
  {
    goto L610;
  }
  if (dls001_3.kflag <=  - 2)
  {
    rh = min(rh, .2);
  }
  /* ----------------------------------------------------------------------- */
  /* If there is a change of order, reset NQ, L, and the coefficients. */
  /* In any case H is reset according to RH and the YH array is rescaled. */
  /* Then exit from 690 if the step was OK, or redo the step otherwise. */
  /* ----------------------------------------------------------------------- */
  if (newq == dls001_3.nq)
  {
    goto L170;
  }
  L630: dls001_3.nq = newq;
  dls001_3.l = dls001_3.nq + 1;
  iret = 2;
  goto L150;
  /* ----------------------------------------------------------------------- */
  /* Control reaches this section if 3 or more failures have occured. */
  /* If 10 failures have occurred, exit with KFLAG = -1. */
  /* It is assumed that the derivatives that have accumulated in the */
  /* YH array have errors of the wrong order.  Hence the first */
  /* derivative is recomputed, and the order is set to 1.  Then */
  /* H is reduced by a factor of 10, and the step is retried, */
  /* until it succeeds or H reaches HMIN. */
  /* ----------------------------------------------------------------------- */
  L640: if (dls001_3.kflag ==  - 10)
  {
    goto L660;
  }
  rh = .1;
  /* Computing MAX */
  d__1 = dls001_3.hmin / abs(dls001_3.h__);
  rh = max(d__1, rh);
  dls001_3.h__ *= rh;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L645: */
    y[i__] = yh[i__ + yh_dim1];
  }
  (*f)(&neq[1], &dls001_3.tn, &y[1], &savf[1]);
  ++dls001_3.nfe;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L650: */
    yh[i__ + (yh_dim1 << 1)] = dls001_3.h__ * savf[i__];
  }
  dls001_3.ipup = dls001_3.miter;
  dls001_3.ialth = 5;
  if (dls001_3.nq == 1)
  {
    goto L200;
  }
  dls001_3.nq = 1;
  dls001_3.l = 2;
  iret = 3;
  goto L150;
  /* ----------------------------------------------------------------------- */
  /* All returns are made through this section.  H is saved in HOLD */
  /* to allow the caller to change H on the next step. */
  /* ----------------------------------------------------------------------- */
  L660: dls001_3.kflag =  - 1;
  goto L720;
  L670: dls001_3.kflag =  - 2;
  goto L720;
  L680: dls001_3.kflag =  - 3;
  goto L720;
  L690: dls001_3.rmax = 10.;
  L700: r__ = 1. / dls001_3.tesco[dls001_3.nqu *3-2];
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L710: */
    acor[i__] *= r__;
  }
  L720: dls001_3.hold = dls001_3.h__;
  dls001_3.jstart = 1;
  return 0;
  /* ----------------------- End of Subroutine DSTODPK --------------------- */
} /* dstodpk_ */

/* DECK DPKSET */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dpkset_
   (integer *neq, doublereal *y, doublereal *ysv, doublereal *ewt, doublereal
   *ftem, doublereal *savf, doublereal *wm, integer *iwm, S_fp f, S_fp jac)
{
  static doublereal hl0;
  static integer ier;

  /* ----------------------------------------------------------------------- */
  /* DPKSET is called by DSTODPK to interface with the user-supplied */
  /* routine JAC, to compute and process relevant parts of */
  /* the matrix P = I - H*EL(1)*J , where J is the Jacobian df/dy, */
  /* as need for preconditioning matrix operations later. */

  /* In addition to variables described previously, communication */
  /* with DPKSET uses the following: */
  /* Y     = array containing predicted values on entry. */
  /* YSV   = array containing predicted y, to be saved (YH1 in DSTODPK). */
  /* FTEM  = work array of length N (ACOR in DSTODPK). */
  /* SAVF  = array containing f evaluated at predicted y. */
  /* WM    = real work space for matrices. */
  /*         Space for preconditioning data starts at WM(LOCWP). */
  /* IWM   = integer work space. */
  /*         Space for preconditioning data starts at IWM(LOCIWP). */
  /* IERPJ = output error flag,  = 0 if no trouble, .gt. 0 if */
  /*         JAC returned an error flag. */
  /* JCUR  = output flag = 1 to indicate that the Jacobian matrix */
  /*         (or approximation) is now current. */
  /* This routine also uses Common variables EL0, H, TN, IERPJ, JCUR, NJE. */
  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --iwm;
  --wm;
  --savf;
  --ftem;
  --ewt;
  --ysv;
  --y;
  --neq;

  /* Function Body */
  dls001_1.ierpj = 0;
  dls001_1.jcur = 1;
  hl0 = dls001_1.el0 * dls001_1.h__;
  (*jac)((S_fp)f, &neq[1], &dls001_1.tn, &y[1], &ysv[1], &ewt[1], &savf[1], 
    &ftem[1], &hl0, &wm[dlpk01_1.locwp], &iwm[dlpk01_1.lociwp], &ier);
  ++dls001_1.nje;
  if (ier == 0)
  {
    return 0;
  }
  dls001_1.ierpj = 1;
  return 0;
  /* ----------------------- End of Subroutine DPKSET ---------------------- */
} /* dpkset_ */

/* DECK DSOLPK */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dsolpk_
   (integer *neq, doublereal *y, doublereal *savf, doublereal *x, doublereal
   *ewt, doublereal *wm, integer *iwm, S_fp f, U_fp psol)
{
  /* System generated locals */
  integer i__1, i__2;

  /* Local variables */
  static integer lb, lp, lq, lr, lv, lw, lz;
  static doublereal hl0;
  static integer ldl, lwk;
  static integer lpcg, lhes, liom, lgmr, npsl, iflag;
  static doublereal delta;
  static integer maxlp1;

  /* ----------------------------------------------------------------------- */
  /* This routine interfaces to one of DSPIOM, DSPIGMR, DPCG, DPCGS, or */
  /* DUSOL, for the solution of the linear system arising from a Newton */
  /* iteration.  It is called if MITER .ne. 0. */
  /* In addition to variables described elsewhere, */
  /* communication with DSOLPK uses the following variables: */
  /* WM    = real work space containing data for the algorithm */
  /*         (Krylov basis vectors, Hessenberg matrix, etc.) */
  /* IWM   = integer work space containing data for the algorithm */
  /* X     = the right-hand side vector on input, and the solution vector */
  /*         on output, of length N. */
  /* IERSL = output flag (in Common): */
  /*         IERSL =  0 means no trouble occurred. */
  /*         IERSL =  1 means the iterative method failed to converge. */
  /*                    If the preconditioner is out of date, the step */
  /*                    is repeated with a new preconditioner. */
  /*                    Otherwise, the stepsize is reduced (forcing a */
  /*                    new evaluation of the preconditioner) and the */
  /*                    step is repeated. */
  /*         IERSL = -1 means there was a nonrecoverable error in the */
  /*                    iterative solver, and an error exit occurs. */
  /* This routine also uses the Common variables TN, EL0, H, N, MITER, */
  /* DELT, EPCON, SQRTN, RSQRTN, MAXL, KMP, MNEWT, NNI, NLI, NPS, NCFL, */
  /* LOCWP, LOCIWP. */
  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --iwm;
  --wm;
  --ewt;
  --x;
  --savf;
  --y;
  --neq;

  /* Function Body */
  dls001_1.iersl = 0;
  hl0 = dls001_1.h__ * dls001_1.el0;
  delta = dlpk01_1.delt * dlpk01_1.epcon;
  switch (dls001_1.miter)
  {
    case 1:
      goto L100;
    case 2:
      goto L200;
    case 3:
      goto L300;
    case 4:
      goto L400;
    case 5:
      goto L900;
    case 6:
      goto L900;
    case 7:
      goto L900;
    case 8:
      goto L900;
    case 9:
      goto L900;
  }
  /* ----------------------------------------------------------------------- */
  /* Use the SPIOM algorithm to solve the linear system P*x = -f. */
  /* ----------------------------------------------------------------------- */
  L100: lv = 1;
  lb = lv + dls001_1.n * dlpk01_1.maxl;
  lhes = lb + dls001_1.n;
  lwk = lhes + dlpk01_1.maxl * dlpk01_1.maxl;
  dcopy_(&dls001_1.n, &x[1], &c__1, &wm[lb], &c__1);
  dscal_(&dls001_1.n, &dlpk01_1.rsqrtn, &ewt[1], &c__1);
  dspiom_(&neq[1], &dls001_1.tn, &y[1], &savf[1], &wm[lb], &ewt[1], &dls001_1.n,
    &dlpk01_1.maxl, &dlpk01_1.kmp, &delta, &hl0, &dlpk01_1.jpre,
    &dlpk01_1.mnewt, (S_fp)f, (U_fp)psol, &npsl, &x[1],  &wm[lv], &wm[lhes],
    &iwm[1], &liom, &wm[dlpk01_1.locwp], &iwm[dlpk01_1.lociwp], &wm[lwk],
    &iflag);
  ++dlpk01_1.nni;
  dlpk01_1.nli += liom;
  dlpk01_1.nps += npsl;
  dscal_(&dls001_1.n, &dlpk01_1.sqrtn, &ewt[1], &c__1);
  if (iflag != 0)
  {
    ++dlpk01_1.ncfl;
  }
  if (iflag >= 2)
  {
    dls001_1.iersl = 1;
  }
  if (iflag < 0)
  {
    dls001_1.iersl =  - 1;
  }
  return 0;
  /* ----------------------------------------------------------------------- */
  /* Use the SPIGMR algorithm to solve the linear system P*x = -f. */
  /* ----------------------------------------------------------------------- */
  L200: maxlp1 = dlpk01_1.maxl + 1;
  lv = 1;
  lb = lv + dls001_1.n * dlpk01_1.maxl;
  lhes = lb + dls001_1.n + 1;
  lq = lhes + dlpk01_1.maxl * maxlp1;
  lwk = lq + (dlpk01_1.maxl << 1);
  /* Computing MIN */
  i__1 = 1, i__2 = dlpk01_1.maxl - dlpk01_1.kmp;
  ldl = lwk + min(i__1, i__2) *dls001_1.n;
  dcopy_(&dls001_1.n, &x[1], &c__1, &wm[lb], &c__1);
  dscal_(&dls001_1.n, &dlpk01_1.rsqrtn, &ewt[1], &c__1);
  dspigmr_(&neq[1], &dls001_1.tn, &y[1], &savf[1], &wm[lb], &ewt[1],
    &dls001_1.n, &dlpk01_1.maxl, &maxlp1, &dlpk01_1.kmp, &delta, &hl0, 
    &dlpk01_1.jpre, &dlpk01_1.mnewt, (S_fp)f, (U_fp)psol, &npsl, &x[1], &wm[lv],
    &wm[lhes], &wm[lq], &lgmr, &wm[dlpk01_1.locwp], &iwm[dlpk01_1.lociwp],
    &wm[lwk], &wm[ldl], &iflag);
  ++dlpk01_1.nni;
  dlpk01_1.nli += lgmr;
  dlpk01_1.nps += npsl;
  dscal_(&dls001_1.n, &dlpk01_1.sqrtn, &ewt[1], &c__1);
  if (iflag != 0)
  {
    ++dlpk01_1.ncfl;
  }
  if (iflag >= 2)
  {
    dls001_1.iersl = 1;
  }
  if (iflag < 0)
  {
    dls001_1.iersl =  - 1;
  }
  return 0;
  /* ----------------------------------------------------------------------- */
  /* Use DPCG to solve the linear system P*x = -f */
  /* ----------------------------------------------------------------------- */
  L300: lr = 1;
  lp = lr + dls001_1.n;
  lw = lp + dls001_1.n;
  lz = lw + dls001_1.n;
  lwk = lz + dls001_1.n;
  dcopy_(&dls001_1.n, &x[1], &c__1, &wm[lr], &c__1);
  dpcg_(&neq[1], &dls001_1.tn, &y[1], &savf[1], &wm[lr], &ewt[1], &dls001_1.n,
    &dlpk01_1.maxl, &delta, &hl0, &dlpk01_1.jpre, &dlpk01_1.mnewt, (S_fp)f, 
    (U_fp)psol, &npsl, &x[1], &wm[lp], &wm[lw], &wm[lz], &lpcg,
    &wm[dlpk01_1.locwp], &iwm[dlpk01_1.lociwp], &wm[lwk], &iflag);
  ++dlpk01_1.nni;
  dlpk01_1.nli += lpcg;
  dlpk01_1.nps += npsl;
  if (iflag != 0)
  {
    ++dlpk01_1.ncfl;
  }
  if (iflag >= 2)
  {
    dls001_1.iersl = 1;
  }
  if (iflag < 0)
  {
    dls001_1.iersl =  - 1;
  }
  return 0;
  /* ----------------------------------------------------------------------- */
  /* Use DPCGS to solve the linear system P*x = -f */
  /* ----------------------------------------------------------------------- */
  L400: lr = 1;
  lp = lr + dls001_1.n;
  lw = lp + dls001_1.n;
  lz = lw + dls001_1.n;
  lwk = lz + dls001_1.n;
  dcopy_(&dls001_1.n, &x[1], &c__1, &wm[lr], &c__1);
  dpcgs_(&neq[1], &dls001_1.tn, &y[1], &savf[1], &wm[lr], &ewt[1], &dls001_1.n,
    &dlpk01_1.maxl, &delta, &hl0, &dlpk01_1.jpre, &dlpk01_1.mnewt, (S_fp)f, 
    (U_fp)psol, &npsl, &x[1], &wm[lp], &wm[lw], &wm[lz], &lpcg,
    &wm[dlpk01_1.locwp], &iwm[dlpk01_1.lociwp], &wm[lwk], &iflag);
  ++dlpk01_1.nni;
  dlpk01_1.nli += lpcg;
  dlpk01_1.nps += npsl;
  if (iflag != 0)
  {
    ++dlpk01_1.ncfl;
  }
  if (iflag >= 2)
  {
    dls001_1.iersl = 1;
  }
  if (iflag < 0)
  {
    dls001_1.iersl =  - 1;
  }
  return 0;
  /* ----------------------------------------------------------------------- */
  /* Use DUSOL, which interfaces to PSOL, to solve the linear system */
  /* (no Krylov iteration). */
  /* ----------------------------------------------------------------------- */
  L900: lb = 1;
  lwk = lb + dls001_1.n;
  dcopy_(&dls001_1.n, &x[1], &c__1, &wm[lb], &c__1);
  dusol_(&neq[1], &dls001_1.tn, &y[1], &savf[1], &wm[lb], &ewt[1], &dls001_1.n,
    &delta, &hl0, &dlpk01_1.mnewt, (U_fp)psol, &npsl, &x[1],
    &wm[dlpk01_1.locwp], &iwm[dlpk01_1.lociwp], &wm[lwk], &iflag);
  ++dlpk01_1.nni;
  dlpk01_1.nps += npsl;
  if (iflag != 0)
  {
    ++dlpk01_1.ncfl;
  }
  if (iflag == 3)
  {
    dls001_1.iersl = 1;
  }
  if (iflag < 0)
  {
    dls001_1.iersl =  - 1;
  }
  return 0;
  /* ----------------------- End of Subroutine DSOLPK ---------------------- */
} /* dsolpk_ */

/* DECK DSPIOM */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dspiom_
   (integer *neq, doublereal *tn, doublereal *y, doublereal *savf, doublereal
   *b, doublereal *wght, integer *n, integer *maxl, integer *kmp, doublereal
   *delta, doublereal *hl0, integer *jpre, integer *mnewt, S_fp f, S_fp psol,
   integer *npsl, doublereal *x, doublereal *v, doublereal *hes, integer *ipvt,
   integer*liom, doublereal *wp, integer *iwp, doublereal *wk, integer *iflag)
{
  /* System generated locals */
  integer v_dim1, v_offset, hes_dim1, hes_offset, i__1, i__2;
  doublereal d__1;

  /* Local variables */
  static integer i__, j, k, ll, lm1, ier;
  static doublereal tem, rho;
  static integer info;
  static doublereal bnrm;
  static doublereal prod, bnrm0;
  static doublereal snormw;

  /* ----------------------------------------------------------------------- */
  /* This routine solves the linear system A * x = b using a scaled */
  /* preconditioned version of the Incomplete Orthogonalization Method. */
  /* An initial guess of x = 0 is assumed. */
  /* ----------------------------------------------------------------------- */

  /*      On entry */

  /*          NEQ = problem size, passed to F and PSOL (NEQ(1) = N). */

  /*           TN = current value of t. */

  /*            Y = array containing current dependent variable vector. */

  /*         SAVF = array containing current value of f(t,y). */

  /*         B    = the right hand side of the system A*x = b. */
  /*                B is also used as work space when computing the */
  /*                final approximation. */
  /*                (B is the same as V(*,MAXL+1) in the call to DSPIOM.) */

  /*         WGHT = array of length N containing scale factors. */
  /*                1/WGHT(i) are the diagonal elements of the diagonal */
  /*                scaling matrix D. */

  /*         N    = the order of the matrix A, and the lengths */
  /*                of the vectors Y, SAVF, B, WGHT, and X. */

  /*         MAXL = the maximum allowable order of the matrix HES. */

  /*          KMP = the number of previous vectors the new vector VNEW */
  /*                must be made orthogonal to.  KMP .le. MAXL. */

  /*        DELTA = tolerance on residuals b - A*x in weighted RMS-norm. */

  /*          HL0 = current value of (step size h) * (coefficient l0). */

  /*         JPRE = preconditioner type flag. */

  /*        MNEWT = Newton iteration counter (.ge. 0). */

  /*           WK = real work array of length N used by DATV and PSOL. */

  /*           WP = real work array used by preconditioner PSOL. */

  /*          IWP = integer work array used by preconditioner PSOL. */

  /*      On return */

  /*         X    = the final computed approximation to the solution */
  /*                of the system A*x = b. */

  /*         V    = the N by (LIOM+1) array containing the LIOM */
  /*                orthogonal vectors V(*,1) to V(*,LIOM). */

  /*         HES  = the LU factorization of the LIOM by LIOM upper */
  /*                Hessenberg matrix whose entries are the */
  /*                scaled inner products of A*V(*,k) and V(*,i). */

  /*         IPVT = an integer array containg pivoting information. */
  /*                It is loaded in DHEFA and used in DHESL. */

  /*         LIOM = the number of iterations performed, and current */
  /*                order of the upper Hessenberg matrix HES. */

  /*         NPSL = the number of calls to PSOL. */

  /*        IFLAG = integer error flag: */
  /*                0 means convergence in LIOM iterations, LIOM.le.MAXL. */
  /*                1 means the convergence test did not pass in MAXL */
  /*                  iterations, but the residual norm is .lt. 1, */
  /*                  or .lt. norm(b) if MNEWT = 0, and so X is computed. */
  /*                2 means the convergence test did not pass in MAXL */
  /*                  iterations, residual .gt. 1, and X is undefined. */
  /*                3 means there was a recoverable error in PSOL */
  /*                  caused by the preconditioner being out of date. */
  /*               -1 means there was a nonrecoverable error in PSOL. */

  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --neq;
  --y;
  --savf;
  --b;
  --wght;
  v_dim1 =  *n;
  v_offset = 1+v_dim1;
  v -= v_offset;
  hes_dim1 =  *maxl;
  hes_offset = 1+hes_dim1;
  hes -= hes_offset;
  --x;
  --ipvt;
  --wp;
  --iwp;
  --wk;

  /* Function Body */
  *iflag = 0;
  *liom = 0;
  *npsl = 0;
  /* ----------------------------------------------------------------------- */
  /* The initial residual is the vector b.  Apply scaling to b, and test */
  /* for an immediate return with X = 0 or X = b. */
  /* ----------------------------------------------------------------------- */
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    v[i__ + v_dim1] = b[i__] *wght[i__];
  }
  bnrm0 = dnrm2_(n, &v[v_offset], &c__1);
  bnrm = bnrm0;
  if (bnrm0 >  *delta)
  {
    goto L30;
  }
  if (*mnewt > 0)
  {
    goto L20;
  }
  dcopy_(n, &b[1], &c__1, &x[1], &c__1);
  return 0;
  L20: i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L25: */
    x[i__] = 0.;
  }
  return 0;
  L30: 
  /* Apply inverse of left preconditioner to vector b. -------------------- */
  ier = 0;
  if (*jpre == 0 ||  *jpre == 2)
  {
    goto L55;
  }
  (*psol)(&neq[1], tn, &y[1], &savf[1], &wk[1], hl0, &wp[1], &iwp[1], &b[1], 
    &c__1, &ier);
  *npsl = 1;
  if (ier != 0)
  {
    goto L300;
  }
  /* Calculate norm of scaled vector V(*,1) and normalize it. ------------- */
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L50: */
    v[i__ + v_dim1] = b[i__] *wght[i__];
  }
  bnrm = dnrm2_(n, &v[v_offset], &c__1);
  *delta *= bnrm / bnrm0;
  L55: tem = 1. / bnrm;
  dscal_(n, &tem, &v[v_dim1 + 1], &c__1);
  /* Zero out the HES array. ---------------------------------------------- */
  i__1 =  *maxl;
  for (j = 1; j <= i__1; ++j)
  {
    i__2 =  *maxl;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L60: */
      hes[i__ + j * hes_dim1] = 0.;
    }
    /* L65: */
  }
  /* ----------------------------------------------------------------------- */
  /* Main loop on LL = l to compute the vectors V(*,2) to V(*,MAXL). */
  /* The running product PROD is needed for the convergence test. */
  /* ----------------------------------------------------------------------- */
  prod = 1.;
  i__1 =  *maxl;
  for (ll = 1; ll <= i__1; ++ll)
  {
    *liom = ll;
    /* ----------------------------------------------------------------------- */
    /* Call routine DATV to compute VNEW = Abar*v(l), where Abar is */
    /* the matrix A with scaling and inverse preconditioner factors applied. */
    /* Call routine DORTHOG to orthogonalize the new vector vnew = V(*,l+1). */
    /* Call routine DHEFA to update the factors of HES. */
    /* ----------------------------------------------------------------------- */
    datv_(&neq[1], &y[1], &savf[1], &v[ll *v_dim1 + 1], &wght[1], &x[1], (S_fp)
      f, (S_fp)psol, &v[(ll + 1) *v_dim1 + 1], &wk[1], &wp[1], &iwp[1], hl0,
      jpre, &ier, npsl);
    if (ier != 0)
    {
      goto L300;
    }
    dorthog_(&v[(ll + 1) *v_dim1 + 1], &v[v_offset], &hes[hes_offset], n,  &ll,
      maxl, kmp, &snormw);
    dhefa_(&hes[hes_offset], maxl, &ll, &ipvt[1], &info, &ll);
    lm1 = ll - 1;
    if (ll > 1 && ipvt[lm1] == lm1)
    {
      prod *= hes[ll + lm1 * hes_dim1];
    }
    if (info != ll)
    {
      goto L70;
    }
    /* ----------------------------------------------------------------------- */
    /* The last pivot in HES was found to be zero. */
    /* If vnew = 0 or l = MAXL, take an error return with IFLAG = 2. */
    /* otherwise, continue the iteration without a convergence test. */
    /* ----------------------------------------------------------------------- */
    if (snormw == 0.)
    {
      goto L120;
    }
    if (ll ==  *maxl)
    {
      goto L120;
    }
    goto L80;
    /* ----------------------------------------------------------------------- */
    /* Update RHO, the estimate of the norm of the residual b - A*x(l). */
    /* test for convergence.  If passed, compute approximation x(l). */
    /* If failed and l .lt. MAXL, then continue iterating. */
    /* ----------------------------------------------------------------------- */
    L70: rho = bnrm * snormw *(d__1 = prod / hes[ll + ll * hes_dim1], abs(d__1))
      ;
    if (rho <=  *delta)
    {
      goto L200;
    }
    if (ll ==  *maxl)
    {
      goto L100;
    }
    /* If l .lt. MAXL, store HES(l+1,l) and normalize the vector v(*,l+1). */
    L80: hes[ll + 1+ll * hes_dim1] = snormw;
    tem = 1. / snormw;
    dscal_(n, &tem, &v[(ll + 1) *v_dim1 + 1], &c__1);
    /* L90: */
  }
  /* ----------------------------------------------------------------------- */
  /* l has reached MAXL without passing the convergence test: */
  /* If RHO is not too large, compute a solution anyway and return with */
  /* IFLAG = 1.  Otherwise return with IFLAG = 2. */
  /* ----------------------------------------------------------------------- */
  L100: if (rho <= 1.)
  {
    goto L150;
  }
  if (rho <= bnrm &&  *mnewt == 0)
  {
    goto L150;
  }
  L120: *iflag = 2;
  return 0;
  L150: *iflag = 1;
  /* ----------------------------------------------------------------------- */
  /* Compute the approximation x(l) to the solution. */
  /* Since the vector X was used as work space, and the initial guess */
  /* of the Newton correction is zero, X must be reset to zero. */
  /* ----------------------------------------------------------------------- */
  L200: ll =  *liom;
  i__1 = ll;
  for (k = 1; k <= i__1; ++k)
  {
    /* L210: */
    b[k] = 0.;
  }
  b[1] = bnrm;
  dhesl_(&hes[hes_offset], maxl, &ll, &ipvt[1], &b[1]);
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    /* L220: */
    x[k] = 0.;
  }
  i__1 = ll;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    daxpy_(n, &b[i__], &v[i__ *v_dim1 + 1], &c__1, &x[1], &c__1);
    /* L230: */
  }
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L240: */
    x[i__] /= wght[i__];
  }
  if (*jpre <= 1)
  {
    return 0;
  }
  (*psol)(&neq[1], tn, &y[1], &savf[1], &wk[1], hl0, &wp[1], &iwp[1], &x[1], 
    &c__2, &ier);
  ++(*npsl);
  if (ier != 0)
  {
    goto L300;
  }
  return 0;
  /* ----------------------------------------------------------------------- */
  /* This block handles error returns forced by routine PSOL. */
  /* ----------------------------------------------------------------------- */
  L300: if (ier < 0)
  {
    *iflag =  - 1;
  }
  if (ier > 0)
  {
    *iflag = 3;
  }
  return 0;
  /* ----------------------- End of Subroutine DSPIOM ---------------------- */
} /* dspiom_ */

/* DECK DATV */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::datv_
   (integer *neq, doublereal *y, doublereal *savf, doublereal *v, doublereal
   *wght, doublereal *ftem, S_fp f, S_fp psol, doublereal *z__, doublereal
   *vtem, doublereal *wp, integer *iwp, doublereal *hl0, integer *jpre, integer
   *ier, integer *npsl)
{
  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__;
  static doublereal fac;
  static doublereal tempn, rnorm;

  /* ----------------------------------------------------------------------- */
  /* This routine computes the product */

  /*   (D-inverse)*(P1-inverse)*(I - hl0*df/dy)*(P2-inverse)*(D*v), */

  /* where D is a diagonal scaling matrix, and P1 and P2 are the */
  /* left and right preconditioning matrices, respectively. */
  /* v is assumed to have WRMS norm equal to 1. */
  /* The product is stored in z.  This is computed by a */
  /* difference quotient, a call to F, and two calls to PSOL. */
  /* ----------------------------------------------------------------------- */

  /*      On entry */

  /*          NEQ = problem size, passed to F and PSOL (NEQ(1) = N). */

  /*            Y = array containing current dependent variable vector. */

  /*         SAVF = array containing current value of f(t,y). */

  /*            V = real array of length N (can be the same array as Z). */

  /*         WGHT = array of length N containing scale factors. */
  /*                1/WGHT(i) are the diagonal elements of the matrix D. */

  /*         FTEM = work array of length N. */

  /*         VTEM = work array of length N used to store the */
  /*                unscaled version of V. */

  /*           WP = real work array used by preconditioner PSOL. */

  /*          IWP = integer work array used by preconditioner PSOL. */

  /*          HL0 = current value of (step size h) * (coefficient l0). */

  /*         JPRE = preconditioner type flag. */


  /*      On return */

  /*            Z = array of length N containing desired scaled */
  /*                matrix-vector product. */

  /*          IER = error flag from PSOL. */

  /*         NPSL = the number of calls to PSOL. */

  /* In addition, this routine uses the Common variables TN, N, NFE. */
  /* ----------------------------------------------------------------------- */

  /* Set VTEM = D * V. */
  /* Parameter adjustments */
  --iwp;
  --wp;
  --vtem;
  --z__;
  --ftem;
  --wght;
  --v;
  --savf;
  --y;
  --neq;

  /* Function Body */
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    vtem[i__] = v[i__] / wght[i__];
  }
  *ier = 0;
  if (*jpre >= 2)
  {
    goto L30;
  }

  /* JPRE = 0 or 1.  Save Y in Z and increment Y by VTEM. */
  dcopy_(&dls001_1.n, &y[1], &c__1, &z__[1], &c__1);
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L20: */
    y[i__] = z__[i__] + vtem[i__];
  }
  fac =  *hl0;
  goto L60;

  /* JPRE = 2 or 3.  Apply inverse of right preconditioner to VTEM. */
  L30: (*psol)(&neq[1], &dls001_1.tn, &y[1], &savf[1], &ftem[1], hl0, &wp[1],
    &iwp[1], &vtem[1], &c__2, ier);
  ++(*npsl);
  if (*ier != 0)
  {
    return 0;
  }
  /* Calculate L-2 norm of (D-inverse) * VTEM. */
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L40: */
    z__[i__] = vtem[i__] *wght[i__];
  }
  tempn = dnrm2_(&dls001_1.n, &z__[1], &c__1);
  rnorm = 1. / tempn;
  /* Save Y in Z and increment Y by VTEM/norm. */
  dcopy_(&dls001_1.n, &y[1], &c__1, &z__[1], &c__1);
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L50: */
    y[i__] = z__[i__] + vtem[i__] *rnorm;
  }
  fac =  *hl0 * tempn;

  /* For all JPRE, call F with incremented Y argument, and restore Y. */
  L60: (*f)(&neq[1], &dls001_1.tn, &y[1], &ftem[1]);
  ++dls001_1.nfe;
  dcopy_(&dls001_1.n, &z__[1], &c__1, &y[1], &c__1);
  /* Set Z = (identity - hl0*Jacobian) * VTEM, using difference quotient. */
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L70: */
    z__[i__] = ftem[i__] - savf[i__];
  }
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L80: */
    z__[i__] = vtem[i__] - fac * z__[i__];
  }
  /* Apply inverse of left preconditioner to Z, if nontrivial. */
  if (*jpre == 0 ||  *jpre == 2)
  {
    goto L85;
  }
  (*psol)(&neq[1], &dls001_1.tn, &y[1], &savf[1], &ftem[1], hl0, &wp[1],
    &iwp[1], &z__[1], &c__1, ier);
  ++(*npsl);
  if (*ier != 0)
  {
    return 0;
  }
  L85: 
  /* Apply D-inverse to Z and return. */
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L90: */
    z__[i__] *= wght[i__];
  }
  return 0;
  /* ----------------------- End of Subroutine DATV ------------------------ */
} /* datv_ */

/* DECK DORTHOG */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider
   ::dorthog_(doublereal *vnew, doublereal *v, doublereal *hes, integer *n,
   integer *ll, integer *ldhes, integer *kmp, doublereal *snormw)
{
  /* System generated locals */
  integer v_dim1, v_offset, hes_dim1, hes_offset, i__1, i__2;
  doublereal d__1, d__2, d__3;

  /* Local variables */
  static integer i__, i0;
  static doublereal arg, tem;
  static doublereal vnrm;
  static doublereal sumdsq;

  /* ----------------------------------------------------------------------- */
  /* This routine orthogonalizes the vector VNEW against the previous */
  /* KMP vectors in the V array.  It uses a modified Gram-Schmidt */
  /* orthogonalization procedure with conditional reorthogonalization. */
  /* This is the version of 28 may 1986. */
  /* ----------------------------------------------------------------------- */

  /*      On entry */

  /*         VNEW = the vector of length N containing a scaled product */
  /*                of the Jacobian and the vector V(*,LL). */

  /*         V    = the N x l array containing the previous LL */
  /*                orthogonal vectors v(*,1) to v(*,LL). */

  /*         HES  = an LL x LL upper Hessenberg matrix containing, */
  /*                in HES(i,k), k.lt.LL, scaled inner products of */
  /*                A*V(*,k) and V(*,i). */

  /*        LDHES = the leading dimension of the HES array. */

  /*         N    = the order of the matrix A, and the length of VNEW. */

  /*         LL   = the current order of the matrix HES. */

  /*          KMP = the number of previous vectors the new vector VNEW */
  /*                must be made orthogonal to (KMP .le. MAXL). */


  /*      On return */

  /*         VNEW = the new vector orthogonal to V(*,i0) to V(*,LL), */
  /*                where i0 = MAX(1, LL-KMP+1). */

  /*         HES  = upper Hessenberg matrix with column LL filled in with */
  /*                scaled inner products of A*V(*,LL) and V(*,i). */

  /*       SNORMW = L-2 norm of VNEW. */

  /* ----------------------------------------------------------------------- */

  /* Get norm of unaltered VNEW for later use. ---------------------------- */
  /* Parameter adjustments */
  --vnew;
  v_dim1 =  *n;
  v_offset = 1+v_dim1;
  v -= v_offset;
  hes_dim1 =  *ldhes;
  hes_offset = 1+hes_dim1;
  hes -= hes_offset;

  /* Function Body */
  vnrm = dnrm2_(n, &vnew[1], &c__1);
  /* ----------------------------------------------------------------------- */
  /* Do modified Gram-Schmidt on VNEW = A*v(LL). */
  /* Scaled inner products give new column of HES. */
  /* Projections of earlier vectors are subtracted from VNEW. */
  /* ----------------------------------------------------------------------- */
  /* Computing MAX */
  i__1 = 1, i__2 =  *ll -  *kmp + 1;
  i0 = max(i__1, i__2);
  i__1 =  *ll;
  for (i__ = i0; i__ <= i__1; ++i__)
  {
    hes[i__ +  *ll * hes_dim1] = ddot_(n, &v[i__ *v_dim1 + 1], &c__1, &vnew[1],
      &c__1);
    tem =  - hes[i__ +  *ll * hes_dim1];
    daxpy_(n, &tem, &v[i__ *v_dim1 + 1], &c__1, &vnew[1], &c__1);
    /* L10: */
  }
  /* ----------------------------------------------------------------------- */
  /* Compute SNORMW = norm of VNEW. */
  /* If VNEW is small compared to its input value (in norm), then */
  /* reorthogonalize VNEW to V(*,1) through V(*,LL). */
  /* Correct if relative correction exceeds 1000*(unit roundoff). */
  /* finally, correct SNORMW using the dot products involved. */
  /* ----------------------------------------------------------------------- */
  *snormw = dnrm2_(n, &vnew[1], &c__1);
  if (vnrm +  *snormw * .001 != vnrm)
  {
    return 0;
  }
  sumdsq = 0.;
  i__1 =  *ll;
  for (i__ = i0; i__ <= i__1; ++i__)
  {
    tem =  - ddot_(n, &v[i__ *v_dim1 + 1], &c__1, &vnew[1], &c__1);
    if (hes[i__ +  *ll * hes_dim1] + tem * .001 == hes[i__ +  *ll * hes_dim1])
    {
      goto L30;
    }
    hes[i__ +  *ll * hes_dim1] -= tem;
    daxpy_(n, &tem, &v[i__ *v_dim1 + 1], &c__1, &vnew[1], &c__1);
    /* Computing 2nd power */
    d__1 = tem;
    sumdsq += d__1 * d__1;
    L30: ;
  }
  if (sumdsq == 0.)
  {
    return 0;
  }
  /* Computing MAX */
  /* Computing 2nd power */
  d__3 =  *snormw;
  d__1 = 0., d__2 = d__3 * d__3 - sumdsq;
  arg = max(d__1, d__2);
  *snormw = sqrt(arg);

  return 0;
  /* ----------------------- End of Subroutine DORTHOG --------------------- */
} /* dorthog_ */

/* DECK DSPIGMR */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider
   ::dspigmr_(integer *neq, doublereal *tn, doublereal *y, doublereal *savf,
   doublereal *b, doublereal *wght, integer *n, integer *maxl, integer *maxlp1,
   integer *kmp, doublereal *delta, doublereal *hl0, integer *jpre, integer
   *mnewt, S_fp f, S_fp psol, integer *npsl, doublereal *x, doublereal *v,
   doublereal *hes, doublereal *q, integer *lgmr, doublereal *wp, integer *iwp,
   doublereal *wk, doublereal *dl, integer *iflag)
{
  /* System generated locals */
  integer v_dim1, v_offset, hes_dim1, hes_offset, i__1, i__2, i__3;
  doublereal d__1;

  /* Local variables */
  static doublereal c__;
  static integer i__, j, k;
  static doublereal s;
  static integer i2, ll, ip1, ier;
  static doublereal tem, rho;
  static integer llp1, info;
  static doublereal bnrm;
  static doublereal prod, bnrm0;
  static doublereal dlnrm;
  static doublereal snormw;

  /* ----------------------------------------------------------------------- */
  /* This routine solves the linear system A * x = b using a scaled */
  /* preconditioned version of the Generalized Minimal Residual method. */
  /* An initial guess of x = 0 is assumed. */
  /* ----------------------------------------------------------------------- */

  /*      On entry */

  /*          NEQ = problem size, passed to F and PSOL (NEQ(1) = N). */

  /*           TN = current value of t. */

  /*            Y = array containing current dependent variable vector. */

  /*         SAVF = array containing current value of f(t,y). */

  /*            B = the right hand side of the system A*x = b. */
  /*                B is also used as work space when computing */
  /*                the final approximation. */
  /*                (B is the same as V(*,MAXL+1) in the call to DSPIGMR.) */

  /*         WGHT = the vector of length N containing the nonzero */
  /*                elements of the diagonal scaling matrix. */

  /*            N = the order of the matrix A, and the lengths */
  /*                of the vectors WGHT, B and X. */

  /*         MAXL = the maximum allowable order of the matrix HES. */

  /*       MAXLP1 = MAXL + 1, used for dynamic dimensioning of HES. */

  /*          KMP = the number of previous vectors the new vector VNEW */
  /*                must be made orthogonal to.  KMP .le. MAXL. */

  /*        DELTA = tolerance on residuals b - A*x in weighted RMS-norm. */

  /*          HL0 = current value of (step size h) * (coefficient l0). */

  /*         JPRE = preconditioner type flag. */

  /*        MNEWT = Newton iteration counter (.ge. 0). */

  /*           WK = real work array used by routine DATV and PSOL. */

  /*           DL = real work array used for calculation of the residual */
  /*                norm RHO when the method is incomplete (KMP .lt. MAXL). */
  /*                Not needed or referenced in complete case (KMP = MAXL). */

  /*           WP = real work array used by preconditioner PSOL. */

  /*          IWP = integer work array used by preconditioner PSOL. */

  /*      On return */

  /*         X    = the final computed approximation to the solution */
  /*                of the system A*x = b. */

  /*         LGMR = the number of iterations performed and */
  /*                the current order of the upper Hessenberg */
  /*                matrix HES. */

  /*         NPSL = the number of calls to PSOL. */

  /*         V    = the N by (LGMR+1) array containing the LGMR */
  /*                orthogonal vectors V(*,1) to V(*,LGMR). */

  /*         HES  = the upper triangular factor of the QR decomposition */
  /*                of the (LGMR+1) by lgmr upper Hessenberg matrix whose */
  /*                entries are the scaled inner-products of A*V(*,i) */
  /*                and V(*,k). */

  /*         Q    = real array of length 2*MAXL containing the components */
  /*                of the Givens rotations used in the QR decomposition */
  /*                of HES.  It is loaded in DHEQR and used in DHELS. */

  /*        IFLAG = integer error flag: */
  /*                0 means convergence in LGMR iterations, LGMR .le. MAXL. */
  /*                1 means the convergence test did not pass in MAXL */
  /*                  iterations, but the residual norm is .lt. 1, */
  /*                  or .lt. norm(b) if MNEWT = 0, and so x is computed. */
  /*                2 means the convergence test did not pass in MAXL */
  /*                  iterations, residual .gt. 1, and X is undefined. */
  /*                3 means there was a recoverable error in PSOL */
  /*                  caused by the preconditioner being out of date. */
  /*               -1 means there was a nonrecoverable error in PSOL. */

  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --neq;
  --y;
  --savf;
  --b;
  --wght;
  v_dim1 =  *n;
  v_offset = 1+v_dim1;
  v -= v_offset;
  hes_dim1 =  *maxlp1;
  hes_offset = 1+hes_dim1;
  hes -= hes_offset;
  --x;
  --q;
  --wp;
  --iwp;
  --wk;
  --dl;

  /* Function Body */
  *iflag = 0;
  *lgmr = 0;
  *npsl = 0;
  /* ----------------------------------------------------------------------- */
  /* The initial residual is the vector b.  Apply scaling to b, and test */
  /* for an immediate return with X = 0 or X = b. */
  /* ----------------------------------------------------------------------- */
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    v[i__ + v_dim1] = b[i__] *wght[i__];
  }
  bnrm0 = dnrm2_(n, &v[v_offset], &c__1);
  bnrm = bnrm0;
  if (bnrm0 >  *delta)
  {
    goto L30;
  }
  if (*mnewt > 0)
  {
    goto L20;
  }
  dcopy_(n, &b[1], &c__1, &x[1], &c__1);
  return 0;
  L20: i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L25: */
    x[i__] = 0.;
  }
  return 0;
  L30: 
  /* Apply inverse of left preconditioner to vector b. -------------------- */
  ier = 0;
  if (*jpre == 0 ||  *jpre == 2)
  {
    goto L55;
  }
  (*psol)(&neq[1], tn, &y[1], &savf[1], &wk[1], hl0, &wp[1], &iwp[1], &b[1], 
    &c__1, &ier);
  *npsl = 1;
  if (ier != 0)
  {
    goto L300;
  }
  /* Calculate norm of scaled vector V(*,1) and normalize it. ------------- */
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L50: */
    v[i__ + v_dim1] = b[i__] *wght[i__];
  }
  bnrm = dnrm2_(n, &v[v_offset], &c__1);
  *delta *= bnrm / bnrm0;
  L55: tem = 1. / bnrm;
  dscal_(n, &tem, &v[v_dim1 + 1], &c__1);
  /* Zero out the HES array. ---------------------------------------------- */
  i__1 =  *maxl;
  for (j = 1; j <= i__1; ++j)
  {
    i__2 =  *maxlp1;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L60: */
      hes[i__ + j * hes_dim1] = 0.;
    }
    /* L65: */
  }
  /* ----------------------------------------------------------------------- */
  /* Main loop to compute the vectors V(*,2) to V(*,MAXL). */
  /* The running product PROD is needed for the convergence test. */
  /* ----------------------------------------------------------------------- */
  prod = 1.;
  i__1 =  *maxl;
  for (ll = 1; ll <= i__1; ++ll)
  {
    *lgmr = ll;
    /* ----------------------------------------------------------------------- */
    /* Call routine DATV to compute VNEW = Abar*v(ll), where Abar is */
    /* the matrix A with scaling and inverse preconditioner factors applied. */
    /* Call routine DORTHOG to orthogonalize the new vector VNEW = V(*,LL+1). */
    /* Call routine DHEQR to update the factors of HES. */
    /* ----------------------------------------------------------------------- */
    datv_(&neq[1], &y[1], &savf[1], &v[ll *v_dim1 + 1], &wght[1], &x[1], (S_fp)
      f, (S_fp)psol, &v[(ll + 1) *v_dim1 + 1], &wk[1], &wp[1], &iwp[1], hl0,
      jpre, &ier, npsl);
    if (ier != 0)
    {
      goto L300;
    }
    dorthog_(&v[(ll + 1) *v_dim1 + 1], &v[v_offset], &hes[hes_offset], n,  &ll,
      maxlp1, kmp, &snormw);
    hes[ll + 1+ll * hes_dim1] = snormw;
    dheqr_(&hes[hes_offset], maxlp1, &ll, &q[1], &info, &ll);
    if (info == ll)
    {
      goto L120;
    }
    /* ----------------------------------------------------------------------- */
    /* Update RHO, the estimate of the norm of the residual b - A*xl. */
    /* If KMP .lt. MAXL, then the vectors V(*,1),...,V(*,LL+1) are not */
    /* necessarily orthogonal for LL .gt. KMP.  The vector DL must then */
    /* be computed, and its norm used in the calculation of RHO. */
    /* ----------------------------------------------------------------------- */
    prod *= q[ll *2];
    rho = (d__1 = prod * bnrm, abs(d__1));
    if (ll >  *kmp &&  *kmp <  *maxl)
    {
      if (ll ==  *kmp + 1)
      {
        dcopy_(n, &v[v_dim1 + 1], &c__1, &dl[1], &c__1);
        i__2 =  *kmp;
        for (i__ = 1; i__ <= i__2; ++i__)
        {
          ip1 = i__ + 1;
          i2 = i__ << 1;
          s = q[i2];
          c__ = q[i2 - 1];
          i__3 =  *n;
          for (k = 1; k <= i__3; ++k)
          {
            /* L70: */
            dl[k] = s * dl[k] + c__ * v[k + ip1 * v_dim1];
          }
          /* L75: */
        }
      }
      s = q[ll *2];
      c__ = q[(ll << 1) - 1] / snormw;
      llp1 = ll + 1;
      i__2 =  *n;
      for (k = 1; k <= i__2; ++k)
      {
        /* L80: */
        dl[k] = s * dl[k] + c__ * v[k + llp1 * v_dim1];
      }
      dlnrm = dnrm2_(n, &dl[1], &c__1);
      rho *= dlnrm;
    }
    /* ----------------------------------------------------------------------- */
    /* Test for convergence.  If passed, compute approximation xl. */
    /* if failed and LL .lt. MAXL, then continue iterating. */
    /* ----------------------------------------------------------------------- */
    if (rho <=  *delta)
    {
      goto L200;
    }
    if (ll ==  *maxl)
    {
      goto L100;
    }
    /* ----------------------------------------------------------------------- */
    /* Rescale so that the norm of V(1,LL+1) is one. */
    /* ----------------------------------------------------------------------- */
    tem = 1. / snormw;
    dscal_(n, &tem, &v[(ll + 1) *v_dim1 + 1], &c__1);
    /* L90: */
  }
  L100: if (rho <= 1.)
  {
    goto L150;
  }
  if (rho <= bnrm &&  *mnewt == 0)
  {
    goto L150;
  }
  L120: *iflag = 2;
  return 0;
  L150: *iflag = 1;
  /* ----------------------------------------------------------------------- */
  /* Compute the approximation xl to the solution. */
  /* Since the vector X was used as work space, and the initial guess */
  /* of the Newton correction is zero, X must be reset to zero. */
  /* ----------------------------------------------------------------------- */
  L200: ll =  *lgmr;
  llp1 = ll + 1;
  i__1 = llp1;
  for (k = 1; k <= i__1; ++k)
  {
    /* L210: */
    b[k] = 0.;
  }
  b[1] = bnrm;
  dhels_(&hes[hes_offset], maxlp1, &ll, &q[1], &b[1]);
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    /* L220: */
    x[k] = 0.;
  }
  i__1 = ll;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    daxpy_(n, &b[i__], &v[i__ *v_dim1 + 1], &c__1, &x[1], &c__1);
    /* L230: */
  }
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L240: */
    x[i__] /= wght[i__];
  }
  if (*jpre <= 1)
  {
    return 0;
  }
  (*psol)(&neq[1], tn, &y[1], &savf[1], &wk[1], hl0, &wp[1], &iwp[1], &x[1], 
    &c__2, &ier);
  ++(*npsl);
  if (ier != 0)
  {
    goto L300;
  }
  return 0;
  /* ----------------------------------------------------------------------- */
  /* This block handles error returns forced by routine PSOL. */
  /* ----------------------------------------------------------------------- */
  L300: if (ier < 0)
  {
    *iflag =  - 1;
  }
  if (ier > 0)
  {
    *iflag = 3;
  }

  return 0;
  /* ----------------------- End of Subroutine DSPIGMR --------------------- */
} /* dspigmr_ */

/* DECK DPCG */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dpcg_
   (integer *neq, doublereal *tn, doublereal *y, doublereal *savf, doublereal
   *r__, doublereal *wght, integer *n, integer *maxl, doublereal *delta,
   doublereal *hl0, integer *jpre, integer *mnewt, S_fp f, S_fp psol, integer
   *npsl, doublereal *x, doublereal *p, doublereal *w, doublereal *z__, integer
   *lpcg, doublereal *wp, integer *iwp, doublereal *wk, integer *iflag)
{
  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__, ier;
  static doublereal ptw, ztr, ztr0, beta;
  static doublereal bnrm, rnrm, alpha;

  /* ----------------------------------------------------------------------- */
  /* This routine computes the solution to the system A*x = b using a */
  /* preconditioned version of the Conjugate Gradient algorithm. */
  /* It is assumed here that the matrix A and the preconditioner */
  /* matrix M are symmetric positive definite or nearly so. */
  /* ----------------------------------------------------------------------- */

  /*      On entry */

  /*          NEQ = problem size, passed to F and PSOL (NEQ(1) = N). */

  /*           TN = current value of t. */

  /*            Y = array containing current dependent variable vector. */

  /*         SAVF = array containing current value of f(t,y). */

  /*            R = the right hand side of the system A*x = b. */

  /*         WGHT = array of length N containing scale factors. */
  /*                1/WGHT(i) are the diagonal elements of the diagonal */
  /*                scaling matrix D. */

  /*            N = the order of the matrix A, and the lengths */
  /*                of the vectors Y, SAVF, R, WGHT, P, W, Z, WK, and X. */

  /*         MAXL = the maximum allowable number of iterates. */

  /*        DELTA = tolerance on residuals b - A*x in weighted RMS-norm. */

  /*          HL0 = current value of (step size h) * (coefficient l0). */

  /*         JPRE = preconditioner type flag. */

  /*        MNEWT = Newton iteration counter (.ge. 0). */

  /*           WK = real work array used by routine DATP. */

  /*           WP = real work array used by preconditioner PSOL. */

  /*          IWP = integer work array used by preconditioner PSOL. */

  /*      On return */

  /*         X    = the final computed approximation to the solution */
  /*                of the system A*x = b. */

  /*         LPCG = the number of iterations performed, and current */
  /*                order of the upper Hessenberg matrix HES. */

  /*         NPSL = the number of calls to PSOL. */

  /*        IFLAG = integer error flag: */
  /*                0 means convergence in LPCG iterations, LPCG .le. MAXL. */
  /*                1 means the convergence test did not pass in MAXL */
  /*                  iterations, but the residual norm is .lt. 1, */
  /*                  or .lt. norm(b) if MNEWT = 0, and so X is computed. */
  /*                2 means the convergence test did not pass in MAXL */
  /*                  iterations, residual .gt. 1, and X is undefined. */
  /*                3 means there was a recoverable error in PSOL */
  /*                  caused by the preconditioner being out of date. */
  /*                4 means there was a zero denominator in the algorithm. */
  /*                  The system matrix or preconditioner matrix is not */
  /*                  sufficiently close to being symmetric pos. definite. */
  /*               -1 means there was a nonrecoverable error in PSOL. */

  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --wk;
  --iwp;
  --wp;
  --z__;
  --w;
  --p;
  --x;
  --wght;
  --r__;
  --savf;
  --y;
  --neq;

  /* Function Body */
  *iflag = 0;
  *npsl = 0;
  *lpcg = 0;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    x[i__] = 0.;
  }
  bnrm = dvnorm_(n, &r__[1], &wght[1]);
  /* Test for immediate return with X = 0 or X = b. ----------------------- */
  if (bnrm >  *delta)
  {
    goto L20;
  }
  if (*mnewt > 0)
  {
    return 0;
  }
  dcopy_(n, &r__[1], &c__1, &x[1], &c__1);
  return 0;

  L20: ztr = 0.;
  /* Loop point for PCG iterations. --------------------------------------- */
  L30: ++(*lpcg);
  dcopy_(n, &r__[1], &c__1, &z__[1], &c__1);
  ier = 0;
  if (*jpre == 0)
  {
    goto L40;
  }
  (*psol)(&neq[1], tn, &y[1], &savf[1], &wk[1], hl0, &wp[1], &iwp[1], &z__[1],
    &c__3, &ier);
  ++(*npsl);
  if (ier != 0)
  {
    goto L100;
  }
  L40: ztr0 = ztr;
  ztr = ddot_(n, &z__[1], &c__1, &r__[1], &c__1);
  if (*lpcg != 1)
  {
    goto L50;
  }
  dcopy_(n, &z__[1], &c__1, &p[1], &c__1);
  goto L70;
  L50: if (ztr0 == 0.)
  {
    goto L200;
  }
  beta = ztr / ztr0;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L60: */
    p[i__] = z__[i__] + beta * p[i__];
  }
  L70: 
  /* ----------------------------------------------------------------------- */
  /*  Call DATP to compute A*p and return the answer in W. */
  /* ----------------------------------------------------------------------- */
  datp_(&neq[1], &y[1], &savf[1], &p[1], &wght[1], hl0, &wk[1], (S_fp)f, &w[1]);

  ptw = ddot_(n, &p[1], &c__1, &w[1], &c__1);
  if (ptw == 0.)
  {
    goto L200;
  }
  alpha = ztr / ptw;
  daxpy_(n, &alpha, &p[1], &c__1, &x[1], &c__1);
  alpha =  - alpha;
  daxpy_(n, &alpha, &w[1], &c__1, &r__[1], &c__1);
  rnrm = dvnorm_(n, &r__[1], &wght[1]);
  if (rnrm <=  *delta)
  {
    return 0;
  }
  if (*lpcg <  *maxl)
  {
    goto L30;
  }
  *iflag = 2;
  if (rnrm <= 1.)
  {
    *iflag = 1;
  }
  if (rnrm <= bnrm &&  *mnewt == 0)
  {
    *iflag = 1;
  }
  return 0;
  /* ----------------------------------------------------------------------- */
  /* This block handles error returns from PSOL. */
  /* ----------------------------------------------------------------------- */
  L100: if (ier < 0)
  {
    *iflag =  - 1;
  }
  if (ier > 0)
  {
    *iflag = 3;
  }
  return 0;
  /* ----------------------------------------------------------------------- */
  /* This block handles division by zero errors. */
  /* ----------------------------------------------------------------------- */
  L200: *iflag = 4;
  return 0;
  /* ----------------------- End of Subroutine DPCG ------------------------ */
} /* dpcg_ */

/* DECK DPCGS */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dpcgs_
   (integer *neq, doublereal *tn, doublereal *y, doublereal *savf, doublereal
   *r__, doublereal *wght, integer *n, integer *maxl, doublereal *delta,
   doublereal *hl0, integer *jpre, integer *mnewt, S_fp f, S_fp psol, integer
   *npsl, doublereal *x, doublereal *p, doublereal *w, doublereal *z__, integer
   *lpcg, doublereal *wp, integer *iwp, doublereal *wk, integer *iflag)
{
  /* System generated locals */
  integer i__1;
  doublereal d__1;

  /* Local variables */
  static integer i__, ier;
  static doublereal ptw, ztr, ztr0, beta;
  static doublereal bnrm, rnrm, alpha;

  /* ----------------------------------------------------------------------- */
  /* This routine computes the solution to the system A*x = b using a */
  /* scaled preconditioned version of the Conjugate Gradient algorithm. */
  /* It is assumed here that the scaled matrix D**-1 * A * D and the */
  /* scaled preconditioner D**-1 * M * D are close to being */
  /* symmetric positive definite. */
  /* ----------------------------------------------------------------------- */

  /*      On entry */

  /*          NEQ = problem size, passed to F and PSOL (NEQ(1) = N). */

  /*           TN = current value of t. */

  /*            Y = array containing current dependent variable vector. */

  /*         SAVF = array containing current value of f(t,y). */

  /*            R = the right hand side of the system A*x = b. */

  /*         WGHT = array of length N containing scale factors. */
  /*                1/WGHT(i) are the diagonal elements of the diagonal */
  /*                scaling matrix D. */

  /*            N = the order of the matrix A, and the lengths */
  /*                of the vectors Y, SAVF, R, WGHT, P, W, Z, WK, and X. */

  /*         MAXL = the maximum allowable number of iterates. */

  /*        DELTA = tolerance on residuals b - A*x in weighted RMS-norm. */

  /*          HL0 = current value of (step size h) * (coefficient l0). */

  /*         JPRE = preconditioner type flag. */

  /*        MNEWT = Newton iteration counter (.ge. 0). */

  /*           WK = real work array used by routine DATP. */

  /*           WP = real work array used by preconditioner PSOL. */

  /*          IWP = integer work array used by preconditioner PSOL. */

  /*      On return */

  /*         X    = the final computed approximation to the solution */
  /*                of the system A*x = b. */

  /*         LPCG = the number of iterations performed, and current */
  /*                order of the upper Hessenberg matrix HES. */

  /*         NPSL = the number of calls to PSOL. */

  /*        IFLAG = integer error flag: */
  /*                0 means convergence in LPCG iterations, LPCG .le. MAXL. */
  /*                1 means the convergence test did not pass in MAXL */
  /*                  iterations, but the residual norm is .lt. 1, */
  /*                  or .lt. norm(b) if MNEWT = 0, and so X is computed. */
  /*                2 means the convergence test did not pass in MAXL */
  /*                  iterations, residual .gt. 1, and X is undefined. */
  /*                3 means there was a recoverable error in PSOL */
  /*                  caused by the preconditioner being out of date. */
  /*                4 means there was a zero denominator in the algorithm. */
  /*                  the scaled matrix or scaled preconditioner is not */
  /*                  sufficiently close to being symmetric pos. definite. */
  /*               -1 means there was a nonrecoverable error in PSOL. */

  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --wk;
  --iwp;
  --wp;
  --z__;
  --w;
  --p;
  --x;
  --wght;
  --r__;
  --savf;
  --y;
  --neq;

  /* Function Body */
  *iflag = 0;
  *npsl = 0;
  *lpcg = 0;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    x[i__] = 0.;
  }
  bnrm = dvnorm_(n, &r__[1], &wght[1]);
  /* Test for immediate return with X = 0 or X = b. ----------------------- */
  if (bnrm >  *delta)
  {
    goto L20;
  }
  if (*mnewt > 0)
  {
    return 0;
  }
  dcopy_(n, &r__[1], &c__1, &x[1], &c__1);
  return 0;

  L20: ztr = 0.;
  /* Loop point for PCG iterations. --------------------------------------- */
  L30: ++(*lpcg);
  dcopy_(n, &r__[1], &c__1, &z__[1], &c__1);
  ier = 0;
  if (*jpre == 0)
  {
    goto L40;
  }
  (*psol)(&neq[1], tn, &y[1], &savf[1], &wk[1], hl0, &wp[1], &iwp[1], &z__[1],
    &c__3, &ier);
  ++(*npsl);
  if (ier != 0)
  {
    goto L100;
  }
  L40: ztr0 = ztr;
  ztr = 0.;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L45: */
    /* Computing 2nd power */
    d__1 = wght[i__];
    ztr += z__[i__] *r__[i__]*(d__1 *d__1);
  }
  if (*lpcg != 1)
  {
    goto L50;
  }
  dcopy_(n, &z__[1], &c__1, &p[1], &c__1);
  goto L70;
  L50: if (ztr0 == 0.)
  {
    goto L200;
  }
  beta = ztr / ztr0;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L60: */
    p[i__] = z__[i__] + beta * p[i__];
  }
  L70: 
  /* ----------------------------------------------------------------------- */
  /*  Call DATP to compute A*p and return the answer in W. */
  /* ----------------------------------------------------------------------- */
  datp_(&neq[1], &y[1], &savf[1], &p[1], &wght[1], hl0, &wk[1], (S_fp)f, &w[1]);

  ptw = 0.;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L80: */
    /* Computing 2nd power */
    d__1 = wght[i__];
    ptw += p[i__] *w[i__]*(d__1 *d__1);
  }
  if (ptw == 0.)
  {
    goto L200;
  }
  alpha = ztr / ptw;
  daxpy_(n, &alpha, &p[1], &c__1, &x[1], &c__1);
  alpha =  - alpha;
  daxpy_(n, &alpha, &w[1], &c__1, &r__[1], &c__1);
  rnrm = dvnorm_(n, &r__[1], &wght[1]);
  if (rnrm <=  *delta)
  {
    return 0;
  }
  if (*lpcg <  *maxl)
  {
    goto L30;
  }
  *iflag = 2;
  if (rnrm <= 1.)
  {
    *iflag = 1;
  }
  if (rnrm <= bnrm &&  *mnewt == 0)
  {
    *iflag = 1;
  }
  return 0;
  /* ----------------------------------------------------------------------- */
  /* This block handles error returns from PSOL. */
  /* ----------------------------------------------------------------------- */
  L100: if (ier < 0)
  {
    *iflag =  - 1;
  }
  if (ier > 0)
  {
    *iflag = 3;
  }
  return 0;
  /* ----------------------------------------------------------------------- */
  /* This block handles division by zero errors. */
  /* ----------------------------------------------------------------------- */
  L200: *iflag = 4;
  return 0;
  /* ----------------------- End of Subroutine DPCGS ----------------------- */
} /* dpcgs_ */

/* DECK DATP */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::datp_
   (integer *neq, doublereal *y, doublereal *savf, doublereal *p, doublereal
   *wght, doublereal *hl0, doublereal *wk, S_fp f, doublereal *w)
{
  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__;
  static doublereal fac, pnrm;
  static doublereal rpnrm;

  /* ----------------------------------------------------------------------- */
  /* This routine computes the product */

  /*              w = (I - hl0*df/dy)*p */

  /* This is computed by a call to F and a difference quotient. */
  /* ----------------------------------------------------------------------- */

  /*      On entry */

  /*          NEQ = problem size, passed to F and PSOL (NEQ(1) = N). */

  /*            Y = array containing current dependent variable vector. */

  /*         SAVF = array containing current value of f(t,y). */

  /*            P = real array of length N. */

  /*         WGHT = array of length N containing scale factors. */
  /*                1/WGHT(i) are the diagonal elements of the matrix D. */

  /*           WK = work array of length N. */

  /*      On return */


  /*            W = array of length N containing desired */
  /*                matrix-vector product. */

  /* In addition, this routine uses the Common variables TN, N, NFE. */
  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --w;
  --wk;
  --wght;
  --p;
  --savf;
  --y;
  --neq;

  /* Function Body */
  pnrm = dvnorm_(&dls001_1.n, &p[1], &wght[1]);
  rpnrm = 1. / pnrm;
  dcopy_(&dls001_1.n, &y[1], &c__1, &w[1], &c__1);
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L20: */
    y[i__] = w[i__] + p[i__] *rpnrm;
  }
  (*f)(&neq[1], &dls001_1.tn, &y[1], &wk[1]);
  ++dls001_1.nfe;
  dcopy_(&dls001_1.n, &w[1], &c__1, &y[1], &c__1);
  fac =  *hl0 * pnrm;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L40: */
    w[i__] = p[i__] - fac *(wk[i__] - savf[i__]);
  }
  return 0;
  /* ----------------------- End of Subroutine DATP ------------------------ */
} /* datp_ */

/* DECK DUSOL */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dusol_
   (integer *neq, doublereal *tn, doublereal *y, doublereal *savf, doublereal
   *b, doublereal *wght, integer *n, doublereal *delta, doublereal *hl0,
   integer *mnewt, S_fp psol, integer *npsl, doublereal *x, doublereal *wp,
   integer *iwp, doublereal *wk, integer *iflag)
{
  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__, ier;
  static doublereal bnrm;

  /* ----------------------------------------------------------------------- */
  /* This routine solves the linear system A * x = b using only a call */
  /* to the user-supplied routine PSOL (no Krylov iteration). */
  /* If the norm of the right-hand side vector b is smaller than DELTA, */
  /* the vector X returned is X = b (if MNEWT = 0) or X = 0 otherwise. */
  /* PSOL is called with an LR argument of 0. */
  /* ----------------------------------------------------------------------- */

  /*      On entry */

  /*          NEQ = problem size, passed to F and PSOL (NEQ(1) = N). */

  /*           TN = current value of t. */

  /*            Y = array containing current dependent variable vector. */

  /*         SAVF = array containing current value of f(t,y). */

  /*            B = the right hand side of the system A*x = b. */

  /*         WGHT = the vector of length N containing the nonzero */
  /*                elements of the diagonal scaling matrix. */

  /*            N = the order of the matrix A, and the lengths */
  /*                of the vectors WGHT, B and X. */

  /*        DELTA = tolerance on residuals b - A*x in weighted RMS-norm. */

  /*          HL0 = current value of (step size h) * (coefficient l0). */

  /*        MNEWT = Newton iteration counter (.ge. 0). */

  /*           WK = real work array used by PSOL. */

  /*           WP = real work array used by preconditioner PSOL. */

  /*          IWP = integer work array used by preconditioner PSOL. */

  /*      On return */

  /*         X    = the final computed approximation to the solution */
  /*                of the system A*x = b. */

  /*         NPSL = the number of calls to PSOL. */

  /*        IFLAG = integer error flag: */
  /*                0 means no trouble occurred. */
  /*                3 means there was a recoverable error in PSOL */
  /*                  caused by the preconditioner being out of date. */
  /*               -1 means there was a nonrecoverable error in PSOL. */

  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --wk;
  --iwp;
  --wp;
  --x;
  --wght;
  --b;
  --savf;
  --y;
  --neq;

  /* Function Body */
  *iflag = 0;
  *npsl = 0;
  /* ----------------------------------------------------------------------- */
  /* Test for an immediate return with X = 0 or X = b. */
  /* ----------------------------------------------------------------------- */
  bnrm = dvnorm_(n, &b[1], &wght[1]);
  if (bnrm >  *delta)
  {
    goto L30;
  }
  if (*mnewt > 0)
  {
    goto L10;
  }
  dcopy_(n, &b[1], &c__1, &x[1], &c__1);
  return 0;
  L10: i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L20: */
    x[i__] = 0.;
  }
  return 0;
  /* Make call to PSOL and copy result from B to X. ----------------------- */
  L30: ier = 0;
  (*psol)(&neq[1], tn, &y[1], &savf[1], &wk[1], hl0, &wp[1], &iwp[1], &b[1], 
    &c__0, &ier);
  *npsl = 1;
  if (ier != 0)
  {
    goto L100;
  }
  dcopy_(n, &b[1], &c__1, &x[1], &c__1);
  return 0;
  /* ----------------------------------------------------------------------- */
  /* This block handles error returns forced by routine PSOL. */
  /* ----------------------------------------------------------------------- */
  L100: if (ier < 0)
  {
    *iflag =  - 1;
  }
  if (ier > 0)
  {
    *iflag = 3;
  }
  return 0;
  /* ----------------------- End of Subroutine DUSOL ----------------------- */
} /* dusol_ */

/* DECK DSRCPK */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dsrcpk_
   (doublereal *rsav, integer *isav, integer *job)
{
  /* Initialized data */

  static integer lenrls = 218;
  static integer lenils = 37;
  static integer lenrlp = 4;
  static integer lenilp = 13;

  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__;

  /* ----------------------------------------------------------------------- */
  /* This routine saves or restores (depending on JOB) the contents of */
  /* the Common blocks DLS001, DLPK01, which are used */
  /* internally by the DLSODPK solver. */

  /* RSAV = real array of length 222 or more. */
  /* ISAV = integer array of length 50 or more. */
  /* JOB  = flag indicating to save or restore the Common blocks: */
  /*        JOB  = 1 if Common is to be saved (written to RSAV/ISAV) */
  /*        JOB  = 2 if Common is to be restored (read from RSAV/ISAV) */
  /*        A call with JOB = 2 presumes a prior call with JOB = 1. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --isav;
  --rsav;

  /* Function Body */

  if (*job == 2)
  {
    goto L100;
  }
  dcopy_(&lenrls, dls001_2.rls, &c__1, &rsav[1], &c__1);
  dcopy_(&lenrlp, dlpk01_2.rlsp, &c__1, &rsav[lenrls + 1], &c__1);
  i__1 = lenils;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L20: */
    isav[i__] = dls001_2.ils[i__ - 1];
  }
  i__1 = lenilp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L40: */
    isav[lenils + i__] = dlpk01_2.ilsp[i__ - 1];
  }
  return 0;

  L100: dcopy_(&lenrls, &rsav[1], &c__1, dls001_2.rls, &c__1);
  dcopy_(&lenrlp, &rsav[lenrls + 1], &c__1, dlpk01_2.rlsp, &c__1);
  i__1 = lenils;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L120: */
    dls001_2.ils[i__ - 1] = isav[i__];
  }
  i__1 = lenilp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L140: */
    dlpk01_2.ilsp[i__ - 1] = isav[lenils + i__];
  }
  return 0;
  /* ----------------------- End of Subroutine DSRCPK ---------------------- */
} /* dsrcpk_ */

/* DECK DHEFA */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dhefa_
   (doublereal *a, integer *lda, integer *n, integer *ipvt, integer *info,
   integer *job)
{
  /* System generated locals */
  integer a_dim1, a_offset, i__1, i__2, i__3;

  /* Local variables */
  static integer j, k, l;
  static doublereal t;
  static integer km1, kp1, nm1;

  /* ----------------------------------------------------------------------- */
  /*     This routine is a modification of the LINPACK routine DGEFA and */
  /*     performs an LU decomposition of an upper Hessenberg matrix A. */
  /*     There are two options available: */

  /*          (1)  performing a fresh factorization */
  /*          (2)  updating the LU factors by adding a row and a */
  /*               column to the matrix A. */
  /* ----------------------------------------------------------------------- */
  /*     DHEFA factors an upper Hessenberg matrix by elimination. */

  /*     On entry */

  /*        A       DOUBLE PRECISION(LDA, N) */
  /*                the matrix to be factored. */

  /*        LDA     INTEGER */
  /*                the leading dimension of the array  A . */

  /*        N       INTEGER */
  /*                the order of the matrix  A . */

  /*        JOB     INTEGER */
  /*                JOB = 1    means that a fresh factorization of the */
  /*                           matrix A is desired. */
  /*                JOB .ge. 2 means that the current factorization of A */
  /*                           will be updated by the addition of a row */
  /*                           and a column. */

  /*     On return */

  /*        A       an upper triangular matrix and the multipliers */
  /*                which were used to obtain it. */
  /*                The factorization can be written  A = L*U  where */
  /*                L  is a product of permutation and unit lower */
  /*                triangular matrices and  U  is upper triangular. */

  /*        IPVT    INTEGER(N) */
  /*                an integer vector of pivot indices. */

  /*        INFO    INTEGER */
  /*                = 0  normal value. */
  /*                = k  if  U(k,k) .eq. 0.0 .  This is not an error */
  /*                     condition for this subroutine, but it does */
  /*                     indicate that DHESL will divide by zero if called. */

  /*     Modification of LINPACK, by Peter Brown, LLNL. */
  /*     Written 7/20/83.  This version dated 6/20/01. */

  /*     BLAS called: DAXPY, IDAMAX */
  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  a_dim1 =  *lda;
  a_offset = 1+a_dim1;
  a -= a_offset;
  --ipvt;

  /* Function Body */
  if (*job > 1)
  {
    goto L80;
  }

  /* A new facorization is desired.  This is essentially the LINPACK */
  /* code with the exception that we know there is only one nonzero */
  /* element below the main diagonal. */

  /*     Gaussian elimination with partial pivoting */

  *info = 0;
  nm1 =  *n - 1;
  if (nm1 < 1)
  {
    goto L70;
  }
  i__1 = nm1;
  for (k = 1; k <= i__1; ++k)
  {
    kp1 = k + 1;

    /*        Find L = pivot index */

    l = idamax_(&c__2, &a[k + k * a_dim1], &c__1) + k - 1;
    ipvt[k] = l;

    /*        Zero pivot implies this column already triangularized */

    if (a[l + k * a_dim1] == 0.)
    {
      goto L40;
    }

    /*           Interchange if necessary */

    if (l == k)
    {
      goto L10;
    }
    t = a[l + k * a_dim1];
    a[l + k * a_dim1] = a[k + k * a_dim1];
    a[k + k * a_dim1] = t;
    L10: 

    /*           Compute multipliers */

    t =  - 1. / a[k + k * a_dim1];
    a[k + 1+k * a_dim1] *= t;

    /*           Row elimination with column indexing */

    i__2 =  *n;
    for (j = kp1; j <= i__2; ++j)
    {
      t = a[l + j * a_dim1];
      if (l == k)
      {
        goto L20;
      }
      a[l + j * a_dim1] = a[k + j * a_dim1];
      a[k + j * a_dim1] = t;
      L20: i__3 =  *n - k;
      daxpy_(&i__3, &t, &a[k + 1+k * a_dim1], &c__1, &a[k + 1+j * a_dim1],
        &c__1);
      /* L30: */
    }
    goto L50;
    L40: *info = k;
    L50: 
    /* L60: */
    ;
  }
  L70: ipvt[ *n] =  *n;
  if (a[ *n +  *n * a_dim1] == 0.)
  {
    *info =  *n;
  }
  return 0;

  /* The old factorization of A will be updated.  A row and a column */
  /* has been added to the matrix A. */
  /* N-1 is now the old order of the matrix. */

  L80: nm1 =  *n - 1;

  /* Perform row interchanges on the elements of the new column, and */
  /* perform elimination operations on the elements using the multipliers. */

  if (nm1 <= 1)
  {
    goto L105;
  }
  i__1 = nm1;
  for (k = 2; k <= i__1; ++k)
  {
    km1 = k - 1;
    l = ipvt[km1];
    t = a[l +  *n * a_dim1];
    if (l == km1)
    {
      goto L90;
    }
    a[l +  *n * a_dim1] = a[km1 +  *n * a_dim1];
    a[km1 +  *n * a_dim1] = t;
    L90: a[k +  *n * a_dim1] += a[k + km1 * a_dim1] *t;
    /* L100: */
  }
  L105: 

  /* Complete update of factorization by decomposing last 2x2 block. */

  *info = 0;

  /*        Find L = pivot index */

  l = idamax_(&c__2, &a[nm1 + nm1 * a_dim1], &c__1) + nm1 - 1;
  ipvt[nm1] = l;

  /*        Zero pivot implies this column already triangularized */

  if (a[l + nm1 * a_dim1] == 0.)
  {
    goto L140;
  }

  /*           Interchange if necessary */

  if (l == nm1)
  {
    goto L110;
  }
  t = a[l + nm1 * a_dim1];
  a[l + nm1 * a_dim1] = a[nm1 + nm1 * a_dim1];
  a[nm1 + nm1 * a_dim1] = t;
  L110: 

  /*           Compute multipliers */

  t =  - 1. / a[nm1 + nm1 * a_dim1];
  a[ *n + nm1 * a_dim1] *= t;

  /*           Row elimination with column indexing */

  t = a[l +  *n * a_dim1];
  if (l == nm1)
  {
    goto L120;
  }
  a[l +  *n * a_dim1] = a[nm1 +  *n * a_dim1];
  a[nm1 +  *n * a_dim1] = t;
  L120: a[ *n +  *n * a_dim1] += t * a[ *n + nm1 * a_dim1];
  goto L150;
  L140: *info = nm1;
  L150: ipvt[ *n] =  *n;
  if (a[ *n +  *n * a_dim1] == 0.)
  {
    *info =  *n;
  }
  return 0;
  /* ----------------------- End of Subroutine DHEFA ----------------------- */
} /* dhefa_ */

/* DECK DHESL */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dhesl_
   (doublereal *a, integer *lda, integer *n, integer *ipvt, doublereal *b)
{
  /* System generated locals */
  integer a_dim1, a_offset, i__1, i__2;

  /* Local variables */
  static integer k, l;
  static doublereal t;
  static integer kb, nm1;

  /* ----------------------------------------------------------------------- */
  /* This is essentially the LINPACK routine DGESL except for changes */
  /* due to the fact that A is an upper Hessenberg matrix. */
  /* ----------------------------------------------------------------------- */
  /*     DHESL solves the real system A * x = b */
  /*     using the factors computed by DHEFA. */

  /*     On entry */

  /*        A       DOUBLE PRECISION(LDA, N) */
  /*                the output from DHEFA. */

  /*        LDA     INTEGER */
  /*                the leading dimension of the array  A . */

  /*        N       INTEGER */
  /*                the order of the matrix  A . */

  /*        IPVT    INTEGER(N) */
  /*                the pivot vector from DHEFA. */

  /*        B       DOUBLE PRECISION(N) */
  /*                the right hand side vector. */

  /*     On return */

  /*        B       the solution vector  x . */

  /*     Modification of LINPACK, by Peter Brown, LLNL. */
  /*     Written 7/20/83.  This version dated 6/20/01. */

  /*     BLAS called: DAXPY */
  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  a_dim1 =  *lda;
  a_offset = 1+a_dim1;
  a -= a_offset;
  --ipvt;
  --b;

  /* Function Body */
  nm1 =  *n - 1;

  /*        Solve  A * x = b */
  /*        First solve  L*y = b */

  if (nm1 < 1)
  {
    goto L30;
  }
  i__1 = nm1;
  for (k = 1; k <= i__1; ++k)
  {
    l = ipvt[k];
    t = b[l];
    if (l == k)
    {
      goto L10;
    }
    b[l] = b[k];
    b[k] = t;
    L10: b[k + 1] += t * a[k + 1+k * a_dim1];
    /* L20: */
  }
  L30: 

  /*        Now solve  U*x = y */

  i__1 =  *n;
  for (kb = 1; kb <= i__1; ++kb)
  {
    k =  *n + 1-kb;
    b[k] /= a[k + k * a_dim1];
    t =  - b[k];
    i__2 = k - 1;
    daxpy_(&i__2, &t, &a[k *a_dim1 + 1], &c__1, &b[1], &c__1);
    /* L40: */
  }
  return 0;
  /* ----------------------- End of Subroutine DHESL ----------------------- */
} /* dhesl_ */

/* DECK DHEQR */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dheqr_
   (doublereal *a, integer *lda, integer *n, doublereal *q, integer *info,
   integer *ijob)
{
  /* System generated locals */
  integer a_dim1, a_offset, i__1, i__2;

  /* Local variables */
  static doublereal c__;
  static integer i__, j, k;
  static doublereal s, t, t1, t2;
  static integer iq, km1, kp1, nm1;

  /* ----------------------------------------------------------------------- */
  /*     This routine performs a QR decomposition of an upper */
  /*     Hessenberg matrix A.  There are two options available: */

  /*          (1)  performing a fresh decomposition */
  /*          (2)  updating the QR factors by adding a row and a */
  /*               column to the matrix A. */
  /* ----------------------------------------------------------------------- */
  /*     DHEQR decomposes an upper Hessenberg matrix by using Givens */
  /*     rotations. */

  /*     On entry */

  /*        A       DOUBLE PRECISION(LDA, N) */
  /*                the matrix to be decomposed. */

  /*        LDA     INTEGER */
  /*                the leading dimension of the array  A . */

  /*        N       INTEGER */
  /*                A is an (N+1) by N Hessenberg matrix. */

  /*        IJOB    INTEGER */
  /*                = 1     means that a fresh decomposition of the */
  /*                        matrix A is desired. */
  /*                .ge. 2  means that the current decomposition of A */
  /*                        will be updated by the addition of a row */
  /*                        and a column. */
  /*     On return */

  /*        A       the upper triangular matrix R. */
  /*                The factorization can be written Q*A = R, where */
  /*                Q is a product of Givens rotations and R is upper */
  /*                triangular. */

  /*        Q       DOUBLE PRECISION(2*N) */
  /*                the factors c and s of each Givens rotation used */
  /*                in decomposing A. */

  /*        INFO    INTEGER */
  /*                = 0  normal value. */
  /*                = k  if  A(k,k) .eq. 0.0 .  This is not an error */
  /*                     condition for this subroutine, but it does */
  /*                     indicate that DHELS will divide by zero */
  /*                     if called. */

  /*     Modification of LINPACK, by Peter Brown, LLNL. */
  /*     Written 1/13/86.  This version dated 6/20/01. */
  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  a_dim1 =  *lda;
  a_offset = 1+a_dim1;
  a -= a_offset;
  --q;

  /* Function Body */
  if (*ijob > 1)
  {
    goto L70;
  }

  /* A new facorization is desired. */

  /*     QR decomposition without pivoting */

  *info = 0;
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    km1 = k - 1;
    kp1 = k + 1;

    /*           Compute kth column of R. */
    /*           First, multiply the kth column of A by the previous */
    /*           k-1 Givens rotations. */

    if (km1 < 1)
    {
      goto L20;
    }
    i__2 = km1;
    for (j = 1; j <= i__2; ++j)
    {
      i__ = (j - 1 << 1) + 1;
      t1 = a[j + k * a_dim1];
      t2 = a[j + 1+k * a_dim1];
      c__ = q[i__];
      s = q[i__ + 1];
      a[j + k * a_dim1] = c__ * t1 - s * t2;
      a[j + 1+k * a_dim1] = s * t1 + c__ * t2;
      /* L10: */
    }

    /*           Compute Givens components c and s */

    L20: iq = (km1 << 1) + 1;
    t1 = a[k + k * a_dim1];
    t2 = a[kp1 + k * a_dim1];
    if (t2 != 0.)
    {
      goto L30;
    }
    c__ = 1.;
    s = 0.;
    goto L50;
    L30: if (abs(t2) < abs(t1))
    {
      goto L40;
    }
    t = t1 / t2;
    s =  - 1. / sqrt(t *t + 1.);
    c__ =  - s * t;
    goto L50;
    L40: t = t2 / t1;
    c__ = 1. / sqrt(t *t + 1.);
    s =  - c__ * t;
    L50: q[iq] = c__;
    q[iq + 1] = s;
    a[k + k * a_dim1] = c__ * t1 - s * t2;
    if (a[k + k * a_dim1] == 0.)
    {
      *info = k;
    }
    /* L60: */
  }
  return 0;

  /* The old factorization of A will be updated.  A row and a column */
  /* has been added to the matrix A. */
  /* N by N-1 is now the old size of the matrix. */

  L70: nm1 =  *n - 1;

  /* Multiply the new column by the N previous Givens rotations. */

  i__1 = nm1;
  for (k = 1; k <= i__1; ++k)
  {
    i__ = (k - 1 << 1) + 1;
    t1 = a[k +  *n * a_dim1];
    t2 = a[k + 1+ *n * a_dim1];
    c__ = q[i__];
    s = q[i__ + 1];
    a[k +  *n * a_dim1] = c__ * t1 - s * t2;
    a[k + 1+ *n * a_dim1] = s * t1 + c__ * t2;
    /* L100: */
  }

  /* Complete update of decomposition by forming last Givens rotation, */
  /* and multiplying it times the column vector (A(N,N), A(N+1,N)). */

  *info = 0;
  t1 = a[ *n +  *n * a_dim1];
  t2 = a[ *n + 1+ *n * a_dim1];
  if (t2 != 0.)
  {
    goto L110;
  }
  c__ = 1.;
  s = 0.;
  goto L130;
  L110: if (abs(t2) < abs(t1))
  {
    goto L120;
  }
  t = t1 / t2;
  s =  - 1. / sqrt(t *t + 1.);
  c__ =  - s * t;
  goto L130;
  L120: t = t2 / t1;
  c__ = 1. / sqrt(t *t + 1.);
  s =  - c__ * t;
  L130: iq = (*n << 1) - 1;
  q[iq] = c__;
  q[iq + 1] = s;
  a[ *n +  *n * a_dim1] = c__ * t1 - s * t2;
  if (a[ *n +  *n * a_dim1] == 0.)
  {
    *info =  *n;
  }
  return 0;
  /* ----------------------- End of Subroutine DHEQR ----------------------- */
} /* dheqr_ */

/* DECK DHELS */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dhels_
   (doublereal *a, integer *lda, integer *n, doublereal *q, doublereal *b)
{
  /* System generated locals */
  integer a_dim1, a_offset, i__1, i__2;

  /* Local variables */
  static doublereal c__;
  static integer k;
  static doublereal s, t, t1, t2;
  static integer kb, iq, kp1;

  /* ----------------------------------------------------------------------- */
  /* This is part of the LINPACK routine DGESL with changes */
  /* due to the fact that A is an upper Hessenberg matrix. */
  /* ----------------------------------------------------------------------- */
  /*     DHELS solves the least squares problem */

  /*           min (b-A*x, b-A*x) */

  /*     using the factors computed by DHEQR. */

  /*     On entry */

  /*        A       DOUBLE PRECISION(LDA, N) */
  /*                the output from DHEQR which contains the upper */
  /*                triangular factor R in the QR decomposition of A. */

  /*        LDA     INTEGER */
  /*                the leading dimension of the array  A . */

  /*        N       INTEGER */
  /*                A is originally an (N+1) by N matrix. */

  /*        Q       DOUBLE PRECISION(2*N) */
  /*                The coefficients of the N givens rotations */
  /*                used in the QR factorization of A. */

  /*        B       DOUBLE PRECISION(N+1) */
  /*                the right hand side vector. */

  /*     On return */

  /*        B       the solution vector  x . */

  /*     Modification of LINPACK, by Peter Brown, LLNL. */
  /*     Written 1/13/86.  This version dated 6/20/01. */

  /*     BLAS called: DAXPY */
  /* ----------------------------------------------------------------------- */

  /*        Minimize (b-A*x, b-A*x) */
  /*        First form Q*b. */

  /* Parameter adjustments */
  a_dim1 =  *lda;
  a_offset = 1+a_dim1;
  a -= a_offset;
  --q;
  --b;

  /* Function Body */
  i__1 =  *n;
  for (k = 1; k <= i__1; ++k)
  {
    kp1 = k + 1;
    iq = (k - 1 << 1) + 1;
    c__ = q[iq];
    s = q[iq + 1];
    t1 = b[k];
    t2 = b[kp1];
    b[k] = c__ * t1 - s * t2;
    b[kp1] = s * t1 + c__ * t2;
    /* L20: */
  }

  /*        Now solve  R*x = Q*b. */

  i__1 =  *n;
  for (kb = 1; kb <= i__1; ++kb)
  {
    k =  *n + 1-kb;
    b[k] /= a[k + k * a_dim1];
    t =  - b[k];
    i__2 = k - 1;
    daxpy_(&i__2, &t, &a[k *a_dim1 + 1], &c__1, &b[1], &c__1);
    /* L40: */
  }
  return 0;
  /* ----------------------- End of Subroutine DHELS ----------------------- */
} /* dhels_ */

/* DECK DLHIN */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dlhin_
   (integer *neq, integer *n, doublereal *t0, doublereal *y0, doublereal *ydot,
   S_fp f, doublereal *tout, doublereal *uround, doublereal *ewt, integer *itol,
   doublereal *atol, doublereal *y, doublereal *temp, doublereal *h0, integer
   *niter, integer *ier)
{
  /* Initialized data */

  static doublereal half = .5;
  static doublereal hun = 100.;
  static doublereal pt1 = .1;
  static doublereal two = 2.;

  /* System generated locals */
  integer i__1;
  doublereal d__1, d__2;

  /* Local variables */
  static integer i__;
  static doublereal t1, hg, afi, hlb, hub, hrat, hnew;
  static integer iter;
  static doublereal delyi, atoli;
  static doublereal tdist, yddnrm;
  static doublereal tround;

  /* ----------------------------------------------------------------------- */
  /* Call sequence input -- NEQ, N, T0, Y0, YDOT, F, TOUT, UROUND, */
  /*                        EWT, ITOL, ATOL, Y, TEMP */
  /* Call sequence output -- H0, NITER, IER */
  /* Common block variables accessed -- None */

  /* Subroutines called by DLHIN: F, DCOPY */
  /* Function routines called by DLHIN: DVNORM */
  /* ----------------------------------------------------------------------- */
  /* This routine computes the step size, H0, to be attempted on the */
  /* first step, when the user has not supplied a value for this. */

  /* First we check that TOUT - T0 differs significantly from zero.  Then */
  /* an iteration is done to approximate the initial second derivative */
  /* and this is used to define H from WRMS-norm(H**2 * yddot / 2) = 1. */
  /* A bias factor of 1/2 is applied to the resulting h. */
  /* The sign of H0 is inferred from the initial values of TOUT and T0. */

  /* Communication with DLHIN is done with the following variables: */

  /* NEQ    = NEQ array of solver, passed to F. */
  /* N      = size of ODE system, input. */
  /* T0     = initial value of independent variable, input. */
  /* Y0     = vector of initial conditions, input. */
  /* YDOT   = vector of initial first derivatives, input. */
  /* F      = name of subroutine for right-hand side f(t,y), input. */
  /* TOUT   = first output value of independent variable */
  /* UROUND = machine unit roundoff */
  /* EWT, ITOL, ATOL = error weights and tolerance parameters */
  /*                   as described in the driver routine, input. */
  /* Y, TEMP = work arrays of length N. */
  /* H0     = step size to be attempted, output. */
  /* NITER  = number of iterations (and of f evaluations) to compute H0, */
  /*          output. */
  /* IER    = the error flag, returned with the value */
  /*          IER = 0  if no trouble occurred, or */
  /*          IER = -1 if TOUT and t0 are considered too close to proceed. */
  /* ----------------------------------------------------------------------- */

  /* Type declarations for local variables -------------------------------- */

  /* ----------------------------------------------------------------------- */
  /* The following Fortran-77 declaration is to cause the values of the */
  /* listed (local) variables to be saved between calls to this integrator. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --temp;
  --y;
  --atol;
  --ewt;
  --ydot;
  --y0;
  --neq;

  /* Function Body */

  *niter = 0;
  tdist = (d__1 =  *tout -  *t0, abs(d__1));
  /* Computing MAX */
  d__1 = abs(*t0), d__2 = abs(*tout);
  tround =  *uround * max(d__1, d__2);
  if (tdist < two *tround)
  {
    goto L100;
  }

  /* Set a lower bound on H based on the roundoff level in T0 and TOUT. --- */
  hlb = hun * tround;
  /* Set an upper bound on H based on TOUT-T0 and the initial Y and YDOT. - */
  hub = pt1 * tdist;
  atoli = atol[1];
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    if (*itol == 2 ||  *itol == 4)
    {
      atoli = atol[i__];
    }
    delyi = pt1 *(d__1 = y0[i__], abs(d__1)) + atoli;
    afi = (d__1 = ydot[i__], abs(d__1));
    if (afi *hub > delyi)
    {
      hub = delyi / afi;
    }
    /* L10: */
  }

  /* Set initial guess for H as geometric mean of upper and lower bounds. - */
  iter = 0;
  hg = sqrt(hlb *hub);
  /* If the bounds have crossed, exit with the mean value. ---------------- */
  if (hub < hlb)
  {
    *h0 = hg;
    goto L90;
  }

  /* Looping point for iteration. ----------------------------------------- */
  L50: 
  /* Estimate the second derivative as a difference quotient in f. -------- */
  t1 =  *t0 + hg;
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L60: */
    y[i__] = y0[i__] + hg * ydot[i__];
  }
  (*f)(&neq[1], &t1, &y[1], &temp[1]);
  i__1 =  *n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L70: */
    temp[i__] = (temp[i__] - ydot[i__]) / hg;
  }
  yddnrm = dvnorm_(n, &temp[1], &ewt[1]);
  /* Get the corresponding new value of H. -------------------------------- */
  if (yddnrm *hub * hub > two)
  {
    hnew = sqrt(two / yddnrm);
  }
  else
  {
    hnew = sqrt(hg *hub);
  }
  ++iter;
  /* ----------------------------------------------------------------------- */
  /* Test the stopping conditions. */
  /* Stop if the new and previous H values differ by a factor of .lt. 2. */
  /* Stop if four iterations have been done.  Also, stop with previous H */
  /* if hnew/hg .gt. 2 after first iteration, as this probably means that */
  /* the second derivative value is bad because of cancellation error. */
  /* ----------------------------------------------------------------------- */
  if (iter >= 4)
  {
    goto L80;
  }
  hrat = hnew / hg;
  if (hrat > half && hrat < two)
  {
    goto L80;
  }
  if (iter >= 2 && hnew > two *hg)
  {
    hnew = hg;
    goto L80;
  }
  hg = hnew;
  goto L50;

  /* Iteration done.  Apply bounds, bias factor, and sign. ---------------- */
  L80: *h0 = hnew * half;
  if (*h0 < hlb)
  {
    *h0 = hlb;
  }
  if (*h0 > hub)
  {
    *h0 = hub;
  }
  L90: d__1 =  *tout -  *t0;
  *h0 = d_sign(h0, &d__1);
  /* Restore Y array from Y0, then exit. ---------------------------------- */
  dcopy_(n, &y0[1], &c__1, &y[1], &c__1);
  *niter = iter;
  *ier = 0;
  return 0;
  /* Error return for TOUT - T0 too small. -------------------------------- */
  L100: *ier =  - 1;
  return 0;
  /* ----------------------- End of Subroutine DLHIN ----------------------- */
} /* dlhin_ */

/* DECK DSTOKA */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dstoka_
   (integer *neq, doublereal *y, doublereal *yh, integer *nyh, doublereal *yh1,
   doublereal *ewt, doublereal *savf, doublereal *savx, doublereal *acor,
   doublereal *wm, integer *iwm, S_fp f, U_fp jac, U_fp psol)
{
  /* System generated locals */
  integer yh_dim1, yh_offset, i__1, i__2;
  doublereal d__1, d__2, d__3;

  /* Local variables */
  static integer i__, j, m;
  static doublereal r__;
  static integer i1, jb;
  static doublereal rh, del, ddn;
  static integer ncf;
  static doublereal drc;
  static integer jok;
  static doublereal dsm, dup, roc, dcon, delp, rhdn, exdn;
  static integer iret, newq;
  static doublereal exsm, exup, rhsm, rhup, told;
  static integer iredo;
  static doublereal stiff;
  static integer nslow;
  static doublereal dfnorm;

  /* ----------------------------------------------------------------------- */
  /* DSTOKA performs one step of the integration of an initial value */
  /* problem for a system of Ordinary Differential Equations. */

  /* This routine was derived from Subroutine DSTODPK in the DLSODPK */
  /* package by the addition of automatic functional/Newton iteration */
  /* switching and logic for re-use of Jacobian data. */
  /* ----------------------------------------------------------------------- */
  /* Note: DSTOKA is independent of the value of the iteration method */
  /* indicator MITER, when this is .ne. 0, and hence is independent */
  /* of the type of chord method used, or the Jacobian structure. */
  /* Communication with DSTOKA is done with the following variables: */

  /* NEQ    = integer array containing problem size in NEQ(1), and */
  /*          passed as the NEQ argument in all calls to F and JAC. */
  /* Y      = an array of length .ge. N used as the Y argument in */
  /*          all calls to F and JAC. */
  /* YH     = an NYH by LMAX array containing the dependent variables */
  /*          and their approximate scaled derivatives, where */
  /*          LMAX = MAXORD + 1.  YH(i,j+1) contains the approximate */
  /*          j-th derivative of y(i), scaled by H**j/factorial(j) */
  /*          (j = 0,1,...,NQ).  On entry for the first step, the first */
  /*          two columns of YH must be set from the initial values. */
  /* NYH    = a constant integer .ge. N, the first dimension of YH. */
  /* YH1    = a one-dimensional array occupying the same space as YH. */
  /* EWT    = an array of length N containing multiplicative weights */
  /*          for local error measurements.  Local errors in y(i) are */
  /*          compared to 1.0/EWT(i) in various error tests. */
  /* SAVF   = an array of working storage, of length N. */
  /*          Also used for input of YH(*,MAXORD+2) when JSTART = -1 */
  /*          and MAXORD .lt. the current order NQ. */
  /* SAVX   = an array of working storage, of length N. */
  /* ACOR   = a work array of length N, used for the accumulated */
  /*          corrections.  On a successful return, ACOR(i) contains */
  /*          the estimated one-step local error in y(i). */
  /* WM,IWM = real and integer work arrays associated with matrix */
  /*          operations in chord iteration (MITER .ne. 0). */
  /* CCMAX  = maximum relative change in H*EL0 before DSETPK is called. */
  /* H      = the step size to be attempted on the next step. */
  /*          H is altered by the error control algorithm during the */
  /*          problem.  H can be either positive or negative, but its */
  /*          sign must remain constant throughout the problem. */
  /* HMIN   = the minimum absolute value of the step size H to be used. */
  /* HMXI   = inverse of the maximum absolute value of H to be used. */
  /*          HMXI = 0.0 is allowed and corresponds to an infinite HMAX. */
  /*          HMIN and HMXI may be changed at any time, but will not */
  /*          take effect until the next change of H is considered. */
  /* TN     = the independent variable. TN is updated on each step taken. */
  /* JSTART = an integer used for input only, with the following */
  /*          values and meanings: */
  /*               0  perform the first step. */
  /*           .gt.0  take a new step continuing from the last. */
  /*              -1  take the next step with a new value of H, MAXORD, */
  /*                    N, METH, MITER, and/or matrix parameters. */
  /*              -2  take the next step with a new value of H, */
  /*                    but with other inputs unchanged. */
  /*          On return, JSTART is set to 1 to facilitate continuation. */
  /* KFLAG  = a completion code with the following meanings: */
  /*               0  the step was succesful. */
  /*              -1  the requested error could not be achieved. */
  /*              -2  corrector convergence could not be achieved. */
  /*              -3  fatal error in DSETPK or DSOLPK. */
  /*          A return with KFLAG = -1 or -2 means either */
  /*          ABS(H) = HMIN or 10 consecutive failures occurred. */
  /*          On a return with KFLAG negative, the values of TN and */
  /*          the YH array are as of the beginning of the last */
  /*          step, and H is the last step size attempted. */
  /* MAXORD = the maximum order of integration method to be allowed. */
  /* MAXCOR = the maximum number of corrector iterations allowed. */
  /* MSBP   = maximum number of steps between DSETPK calls (MITER .gt. 0). */
  /* MXNCF  = maximum number of convergence failures allowed. */
  /* METH/MITER = the method flags.  See description in driver. */
  /* N      = the number of first-order differential equations. */
  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --neq;
  --y;
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --yh1;
  --ewt;
  --savf;
  --savx;
  --acor;
  --wm;
  --iwm;

  /* Function Body */
  dls001_3.kflag = 0;
  told = dls001_3.tn;
  ncf = 0;
  dls001_3.ierpj = 0;
  dls001_3.iersl = 0;
  dls001_3.jcur = 0;
  dls001_3.icf = 0;
  delp = 0.;
  if (dls001_3.jstart > 0)
  {
    goto L200;
  }
  if (dls001_3.jstart ==  - 1)
  {
    goto L100;
  }
  if (dls001_3.jstart ==  - 2)
  {
    goto L160;
  }
  /* ----------------------------------------------------------------------- */
  /* On the first call, the order is set to 1, and other variables are */
  /* initialized.  RMAX is the maximum ratio by which H can be increased */
  /* in a single step.  It is initially 1.E4 to compensate for the small */
  /* initial H, but then is normally equal to 10.  If a failure */
  /* occurs (in corrector convergence or error test), RMAX is set at 2 */
  /* for the next increase. */
  /* ----------------------------------------------------------------------- */
  dls001_3.lmax = dls001_3.maxord + 1;
  dls001_3.nq = 1;
  dls001_3.l = 2;
  dls001_3.ialth = 2;
  dls001_3.rmax = 1e4;
  dls001_3.rc = 0.;
  dls001_3.el0 = 1.;
  dls001_3.crate = .7;
  dls001_3.hold = dls001_3.h__;
  dls001_3.meo = dls001_3.meth;
  dls001_3.nslp = 0;
  dls002_1.nslj = 0;
  dls001_3.ipup = 0;
  iret = 3;
  dls002_1.newt = 0;
  dls002_1.stifr = 0.;
  goto L140;
  /* ----------------------------------------------------------------------- */
  /* The following block handles preliminaries needed when JSTART = -1. */
  /* IPUP is set to MITER to force a matrix update. */
  /* If an order increase is about to be considered (IALTH = 1), */
  /* IALTH is reset to 2 to postpone consideration one more step. */
  /* If the caller has changed METH, DCFODE is called to reset */
  /* the coefficients of the method. */
  /* If the caller has changed MAXORD to a value less than the current */
  /* order NQ, NQ is reduced to MAXORD, and a new H chosen accordingly. */
  /* If H is to be changed, YH must be rescaled. */
  /* If H or METH is being changed, IALTH is reset to L = NQ + 1 */
  /* to prevent further changes in H for that many steps. */
  /* ----------------------------------------------------------------------- */
  L100: dls001_3.ipup = dls001_3.miter;
  dls001_3.lmax = dls001_3.maxord + 1;
  if (dls001_3.ialth == 1)
  {
    dls001_3.ialth = 2;
  }
  if (dls001_3.meth == dls001_3.meo)
  {
    goto L110;
  }
  dcfode_(&dls001_3.meth, dls001_3.elco, dls001_3.tesco);
  dls001_3.meo = dls001_3.meth;
  if (dls001_3.nq > dls001_3.maxord)
  {
    goto L120;
  }
  dls001_3.ialth = dls001_3.l;
  iret = 1;
  goto L150;
  L110: if (dls001_3.nq <= dls001_3.maxord)
  {
    goto L160;
  }
  L120: dls001_3.nq = dls001_3.maxord;
  dls001_3.l = dls001_3.lmax;
  i__1 = dls001_3.l;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L125: */
    dls001_3.el[i__ - 1] = dls001_3.elco[i__ + dls001_3.nq * 13-14];
  }
  dls001_3.nqnyh = dls001_3.nq **nyh;
  dls001_3.rc = dls001_3.rc * dls001_3.el[0] / dls001_3.el0;
  dls001_3.el0 = dls001_3.el[0];
  dls001_3.conit = .5 / (dls001_3.nq + 2);
  dlpk01_1.epcon = dls001_3.conit * dls001_3.tesco[dls001_3.nq *3-2];
  ddn = dvnorm_(&dls001_3.n, &savf[1], &ewt[1]) / dls001_3.tesco[dls001_3.l*3
    -3];
  exdn = 1. / dls001_3.l;
  rhdn = 1. / (pow_dd(&ddn, &exdn) *1.3 + 1.3e-6);
  rh = min(rhdn, 1.);
  iredo = 3;
  if (dls001_3.h__ == dls001_3.hold)
  {
    goto L170;
  }
  /* Computing MIN */
  d__2 = rh, d__3 = (d__1 = dls001_3.h__ / dls001_3.hold, abs(d__1));
  rh = min(d__2, d__3);
  dls001_3.h__ = dls001_3.hold;
  goto L175;
  /* ----------------------------------------------------------------------- */
  /* DCFODE is called to get all the integration coefficients for the */
  /* current METH.  Then the EL vector and related constants are reset */
  /* whenever the order NQ is changed, or at the start of the problem. */
  /* ----------------------------------------------------------------------- */
  L140: dcfode_(&dls001_3.meth, dls001_3.elco, dls001_3.tesco);
  L150: i__1 = dls001_3.l;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L155: */
    dls001_3.el[i__ - 1] = dls001_3.elco[i__ + dls001_3.nq * 13-14];
  }
  dls001_3.nqnyh = dls001_3.nq **nyh;
  dls001_3.rc = dls001_3.rc * dls001_3.el[0] / dls001_3.el0;
  dls001_3.el0 = dls001_3.el[0];
  dls001_3.conit = .5 / (dls001_3.nq + 2);
  dlpk01_1.epcon = dls001_3.conit * dls001_3.tesco[dls001_3.nq *3-2];
  switch (iret)
  {
    case 1:
      goto L160;
    case 2:
      goto L170;
    case 3:
      goto L200;
  }
  /* ----------------------------------------------------------------------- */
  /* If H is being changed, the H ratio RH is checked against */
  /* RMAX, HMIN, and HMXI, and the YH array rescaled.  IALTH is set to */
  /* L = NQ + 1 to prevent a change of H for that many steps, unless */
  /* forced by a convergence or error test failure. */
  /* ----------------------------------------------------------------------- */
  L160: if (dls001_3.h__ == dls001_3.hold)
  {
    goto L200;
  }
  rh = dls001_3.h__ / dls001_3.hold;
  dls001_3.h__ = dls001_3.hold;
  iredo = 3;
  goto L175;
  L170: 
  /* Computing MAX */
  d__1 = rh, d__2 = dls001_3.hmin / abs(dls001_3.h__);
  rh = max(d__1, d__2);
  L175: rh = min(rh, dls001_3.rmax);
  /* Computing MAX */
  d__1 = 1., d__2 = abs(dls001_3.h__) *dls001_3.hmxi * rh;
  rh /= max(d__1, d__2);
  r__ = 1.;
  i__1 = dls001_3.l;
  for (j = 2; j <= i__1; ++j)
  {
    r__ *= rh;
    i__2 = dls001_3.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L180: */
      yh[i__ + j * yh_dim1] *= r__;
    }
  }
  dls001_3.h__ *= rh;
  dls001_3.rc *= rh;
  dls001_3.ialth = dls001_3.l;
  if (iredo == 0)
  {
    goto L690;
  }
  /* ----------------------------------------------------------------------- */
  /* This section computes the predicted values by effectively */
  /* multiplying the YH array by the Pascal triangle matrix. */
  /* The flag IPUP is set according to whether matrix data is involved */
  /* (NEWT .gt. 0 .and. JACFLG .ne. 0) or not, to trigger a call to DSETPK. */
  /* IPUP is set to MITER when RC differs from 1 by more than CCMAX, */
  /* and at least every MSBP steps, when JACFLG = 1. */
  /* RC is the ratio of new to old values of the coefficient  H*EL(1). */
  /* ----------------------------------------------------------------------- */
  L200: if (dls002_1.newt == 0 || dlpk01_1.jacflg == 0)
  {
    drc = 0.;
    dls001_3.ipup = 0;
    dls001_3.crate = .7;
  }
  else
  {
    drc = (d__1 = dls001_3.rc - 1., abs(d__1));
    if (drc > dls001_3.ccmax)
    {
      dls001_3.ipup = dls001_3.miter;
    }
    if (dls001_3.nst >= dls001_3.nslp + dls001_3.msbp)
    {
      dls001_3.ipup = dls001_3.miter;
    }
  }
  dls001_3.tn += dls001_3.h__;
  i1 = dls001_3.nqnyh + 1;
  i__2 = dls001_3.nq;
  for (jb = 1; jb <= i__2; ++jb)
  {
    i1 -=  *nyh;
    /* DIR$ IVDEP */
    i__1 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__1; ++i__)
    {
      /* L210: */
      yh1[i__] += yh1[i__ +  *nyh];
    }
    /* L215: */
  }
  /* ----------------------------------------------------------------------- */
  /* Up to MAXCOR corrector iterations are taken.  A convergence test is */
  /* made on the RMS-norm of each correction, weighted by the error */
  /* weight vector EWT.  The sum of the corrections is accumulated in the */
  /* vector ACOR(i).  The YH array is not altered in the corrector loop. */
  /* Within the corrector loop, an estimated rate of convergence (ROC) */
  /* and a stiffness ratio estimate (STIFF) are kept.  Corresponding */
  /* global estimates are kept as CRATE and stifr. */
  /* ----------------------------------------------------------------------- */
  L220: m = 0;
  dlpk01_1.mnewt = 0;
  stiff = 0.;
  roc = .05;
  nslow = 0;
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L230: */
    y[i__] = yh[i__ + yh_dim1];
  }
  (*f)(&neq[1], &dls001_3.tn, &y[1], &savf[1]);
  ++dls001_3.nfe;
  if (dls002_1.newt == 0 || dls001_3.ipup <= 0)
  {
    goto L250;
  }
  /* ----------------------------------------------------------------------- */
  /* If indicated, DSETPK is called to update any matrix data needed, */
  /* before starting the corrector iteration. */
  /* JOK is set to indicate if the matrix data need not be recomputed. */
  /* IPUP is set to 0 as an indicator that the matrix data is up to date. */
  /* ----------------------------------------------------------------------- */
  jok = 1;
  if (dls001_3.nst == 0 || dls001_3.nst > dls002_1.nslj + 50)
  {
    jok =  - 1;
  }
  if (dls001_3.icf == 1 && drc < .2)
  {
    jok =  - 1;
  }
  if (dls001_3.icf == 2)
  {
    jok =  - 1;
  }
  if (jok ==  - 1)
  {
    dls002_1.nslj = dls001_3.nst;
    ++dls002_1.njev;
  }
  dsetpk_(&neq[1], &y[1], &yh1[1], &ewt[1], &acor[1], &savf[1], &jok, &wm[1],
    &iwm[1], (S_fp)f, (U_fp)jac);
  dls001_3.ipup = 0;
  dls001_3.rc = 1.;
  drc = 0.;
  dls001_3.nslp = dls001_3.nst;
  dls001_3.crate = .7;
  if (dls001_3.ierpj != 0)
  {
    goto L430;
  }
  L250: i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L260: */
    acor[i__] = 0.;
  }
  L270: if (dls002_1.newt != 0)
  {
    goto L350;
  }
  /* ----------------------------------------------------------------------- */
  /* In the case of functional iteration, update Y directly from */
  /* the result of the last function evaluation, and STIFF is set to 1.0. */
  /* ----------------------------------------------------------------------- */
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    savf[i__] = dls001_3.h__ * savf[i__] - yh[i__ + (yh_dim1 << 1)];
    /* L290: */
    y[i__] = savf[i__] - acor[i__];
  }
  del = dvnorm_(&dls001_3.n, &y[1], &ewt[1]);
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    y[i__] = yh[i__ + yh_dim1] + dls001_3.el[0] *savf[i__];
    /* L300: */
    acor[i__] = savf[i__];
  }
  stiff = 1.;
  goto L400;
  /* ----------------------------------------------------------------------- */
  /* In the case of the chord method, compute the corrector error, */
  /* and solve the linear system with that as right-hand side and */
  /* P as coefficient matrix.  STIFF is set to the ratio of the norms */
  /* of the residual and the correction vector. */
  /* ----------------------------------------------------------------------- */
  L350: i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L360: */
    savx[i__] = dls001_3.h__ * savf[i__] - (yh[i__ + (yh_dim1 << 1)] +
      acor[i__]);
  }
  dfnorm = dvnorm_(&dls001_3.n, &savx[1], &ewt[1]);
  dsolpk_(&neq[1], &y[1], &savf[1], &savx[1], &ewt[1], &wm[1], &iwm[1], (S_fp)f,
    (U_fp)psol);
  if (dls001_3.iersl < 0)
  {
    goto L430;
  }
  if (dls001_3.iersl > 0)
  {
    goto L410;
  }
  del = dvnorm_(&dls001_3.n, &savx[1], &ewt[1]);
  if (del > 1e-8)
  {
    /* Computing MAX */
    d__1 = stiff, d__2 = dfnorm / del;
    stiff = max(d__1, d__2);
  }
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    acor[i__] += savx[i__];
    /* L380: */
    y[i__] = yh[i__ + yh_dim1] + dls001_3.el[0] *acor[i__];
  }
  /* ----------------------------------------------------------------------- */
  /* Test for convergence.  If M .gt. 0, an estimate of the convergence */
  /* rate constant is made for the iteration switch, and is also used */
  /* in the convergence test.   If the iteration seems to be diverging or */
  /* converging at a slow rate (.gt. 0.8 more than once), it is stopped. */
  /* ----------------------------------------------------------------------- */
  L400: if (m != 0)
  {
    /* Computing MAX */
    d__1 = .05, d__2 = del / delp;
    roc = max(d__1, d__2);
    /* Computing MAX */
    d__1 = dls001_3.crate * .2;
    dls001_3.crate = max(d__1, roc);
  }
  /* Computing MIN */
  d__1 = 1., d__2 = dls001_3.crate * 1.5;
  dcon = del * min(d__1, d__2) / dlpk01_1.epcon;
  if (dcon <= 1.)
  {
    goto L450;
  }
  ++m;
  if (m == dls001_3.maxcor)
  {
    goto L410;
  }
  if (m >= 2 && del > delp *2.)
  {
    goto L410;
  }
  if (roc > 10.)
  {
    goto L410;
  }
  if (roc > .8)
  {
    ++nslow;
  }
  if (nslow >= 2)
  {
    goto L410;
  }
  dlpk01_1.mnewt = m;
  delp = del;
  (*f)(&neq[1], &dls001_3.tn, &y[1], &savf[1]);
  ++dls001_3.nfe;
  goto L270;
  /* ----------------------------------------------------------------------- */
  /* The corrector iteration failed to converge. */
  /* If functional iteration is being done (NEWT = 0) and MITER .gt. 0 */
  /* (and this is not the first step), then switch to Newton */
  /* (NEWT = MITER), and retry the step.  (Setting STIFR = 1023 insures */
  /* that a switch back will not occur for 10 step attempts.) */
  /* If Newton iteration is being done, but using a preconditioner that */
  /* is out of date (JACFLG .ne. 0 .and. JCUR = 0), then signal for a */
  /* re-evalutation of the preconditioner, and retry the step. */
  /* In all other cases, the YH array is retracted to its values */
  /* before prediction, and H is reduced, if possible.  If H cannot be */
  /* reduced or MXNCF failures have occurred, exit with KFLAG = -2. */
  /* ----------------------------------------------------------------------- */
  L410: dls001_3.icf = 1;
  if (dls002_1.newt == 0)
  {
    if (dls001_3.nst == 0)
    {
      goto L430;
    }
    if (dls001_3.miter == 0)
    {
      goto L430;
    }
    dls002_1.newt = dls001_3.miter;
    dls002_1.stifr = 1023.;
    dls001_3.ipup = dls001_3.miter;
    goto L220;
  }
  if (dls001_3.jcur == 1 || dlpk01_1.jacflg == 0)
  {
    goto L430;
  }
  dls001_3.ipup = dls001_3.miter;
  goto L220;
  L430: dls001_3.icf = 2;
  ++ncf;
  ++dlpk01_1.ncfn;
  dls001_3.rmax = 2.;
  dls001_3.tn = told;
  i1 = dls001_3.nqnyh + 1;
  i__2 = dls001_3.nq;
  for (jb = 1; jb <= i__2; ++jb)
  {
    i1 -=  *nyh;
    /* DIR$ IVDEP */
    i__1 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__1; ++i__)
    {
      /* L440: */
      yh1[i__] -= yh1[i__ +  *nyh];
    }
    /* L445: */
  }
  if (dls001_3.ierpj < 0 || dls001_3.iersl < 0)
  {
    goto L680;
  }
  if (abs(dls001_3.h__) <= dls001_3.hmin *1.00001)
  {
    goto L670;
  }
  if (ncf == dls001_3.mxncf)
  {
    goto L670;
  }
  rh = .5;
  dls001_3.ipup = dls001_3.miter;
  iredo = 1;
  goto L170;
  /* ----------------------------------------------------------------------- */
  /* The corrector has converged.  JCUR is set to 0 to signal that the */
  /* preconditioner involved may need updating later. */
  /* The stiffness ratio STIFR is updated using the latest STIFF value. */
  /* The local error test is made and control passes to statement 500 */
  /* if it fails. */
  /* ----------------------------------------------------------------------- */
  L450: dls001_3.jcur = 0;
  if (dls002_1.newt > 0)
  {
    dls002_1.stifr = (dls002_1.stifr + stiff) *.5;
  }
  if (m == 0)
  {
    dsm = del / dls001_3.tesco[dls001_3.nq *3-2];
  }
  if (m > 0)
  {
    dsm = dvnorm_(&dls001_3.n, &acor[1], &ewt[1]) / dls001_3.tesco[dls001_3.nq
      *3-2];
  }
  if (dsm > 1.)
  {
    goto L500;
  }
  /* ----------------------------------------------------------------------- */
  /* After a successful step, update the YH array. */
  /* If Newton iteration is being done and STIFR is less than 1.5, */
  /* then switch to functional iteration. */
  /* Consider changing H if IALTH = 1.  Otherwise decrease IALTH by 1. */
  /* If IALTH is then 1 and NQ .lt. MAXORD, then ACOR is saved for */
  /* use in a possible order increase on the next step. */
  /* If a change in H is considered, an increase or decrease in order */
  /* by one is considered also.  A change in H is made only if it is by a */
  /* factor of at least 1.1.  If not, IALTH is set to 3 to prevent */
  /* testing for that many steps. */
  /* ----------------------------------------------------------------------- */
  dls001_3.kflag = 0;
  iredo = 0;
  ++dls001_3.nst;
  if (dls002_1.newt == 0)
  {
    ++dls002_1.nsfi;
  }
  if (dls002_1.newt > 0 && dls002_1.stifr < 1.5)
  {
    dls002_1.newt = 0;
  }
  dls001_3.hu = dls001_3.h__;
  dls001_3.nqu = dls001_3.nq;
  i__2 = dls001_3.l;
  for (j = 1; j <= i__2; ++j)
  {
    i__1 = dls001_3.n;
    for (i__ = 1; i__ <= i__1; ++i__)
    {
      /* L470: */
      yh[i__ + j * yh_dim1] += dls001_3.el[j - 1] *acor[i__];
    }
  }
  --dls001_3.ialth;
  if (dls001_3.ialth == 0)
  {
    goto L520;
  }
  if (dls001_3.ialth > 1)
  {
    goto L700;
  }
  if (dls001_3.l == dls001_3.lmax)
  {
    goto L700;
  }
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L490: */
    yh[i__ + dls001_3.lmax * yh_dim1] = acor[i__];
  }
  goto L700;
  /* ----------------------------------------------------------------------- */
  /* The error test failed.  KFLAG keeps track of multiple failures. */
  /* Restore TN and the YH array to their previous values, and prepare */
  /* to try the step again.  Compute the optimum step size for this or */
  /* one lower order.  After 2 or more failures, H is forced to decrease */
  /* by a factor of 0.2 or less. */
  /* ----------------------------------------------------------------------- */
  L500: --dls001_3.kflag;
  dls001_3.tn = told;
  i1 = dls001_3.nqnyh + 1;
  i__1 = dls001_3.nq;
  for (jb = 1; jb <= i__1; ++jb)
  {
    i1 -=  *nyh;
    /* DIR$ IVDEP */
    i__2 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__2; ++i__)
    {
      /* L510: */
      yh1[i__] -= yh1[i__ +  *nyh];
    }
    /* L515: */
  }
  dls001_3.rmax = 2.;
  if (abs(dls001_3.h__) <= dls001_3.hmin *1.00001)
  {
    goto L660;
  }
  if (dls001_3.kflag <=  - 3)
  {
    goto L640;
  }
  iredo = 2;
  rhup = 0.;
  goto L540;
  /* ----------------------------------------------------------------------- */
  /* Regardless of the success or failure of the step, factors */
  /* RHDN, RHSM, and RHUP are computed, by which H could be multiplied */
  /* at order NQ - 1, order NQ, or order NQ + 1, respectively. */
  /* in the case of failure, RHUP = 0.0 to avoid an order increase. */
  /* the largest of these is determined and the new order chosen */
  /* accordingly.  If the order is to be increased, we compute one */
  /* additional scaled derivative. */
  /* ----------------------------------------------------------------------- */
  L520: rhup = 0.;
  if (dls001_3.l == dls001_3.lmax)
  {
    goto L540;
  }
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L530: */
    savf[i__] = acor[i__] - yh[i__ + dls001_3.lmax * yh_dim1];
  }
  dup = dvnorm_(&dls001_3.n, &savf[1], &ewt[1]) / dls001_3.tesco[dls001_3.nq *3
    -1];
  exup = 1. / (dls001_3.l + 1);
  rhup = 1. / (pow_dd(&dup, &exup) *1.4 + 1.4e-6);
  L540: exsm = 1. / dls001_3.l;
  rhsm = 1. / (pow_dd(&dsm, &exsm) *1.2 + 1.2e-6);
  rhdn = 0.;
  if (dls001_3.nq == 1)
  {
    goto L560;
  }
  ddn = dvnorm_(&dls001_3.n, &yh[dls001_3.l *yh_dim1 + 1], &ewt[1]) /
    dls001_3.tesco[dls001_3.nq *3-3];
  exdn = 1. / dls001_3.nq;
  rhdn = 1. / (pow_dd(&ddn, &exdn) *1.3 + 1.3e-6);
  L560: if (rhsm >= rhup)
  {
    goto L570;
  }
  if (rhup > rhdn)
  {
    goto L590;
  }
  goto L580;
  L570: if (rhsm < rhdn)
  {
    goto L580;
  }
  newq = dls001_3.nq;
  rh = rhsm;
  goto L620;
  L580: newq = dls001_3.nq - 1;
  rh = rhdn;
  if (dls001_3.kflag < 0 && rh > 1.)
  {
    rh = 1.;
  }
  goto L620;
  L590: newq = dls001_3.l;
  rh = rhup;
  if (rh < 1.1)
  {
    goto L610;
  }
  r__ = dls001_3.el[dls001_3.l - 1] / dls001_3.l;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L600: */
    yh[i__ + (newq + 1) *yh_dim1] = acor[i__] *r__;
  }
  goto L630;
  L610: dls001_3.ialth = 3;
  goto L700;
  L620: if (dls001_3.kflag == 0 && rh < 1.1)
  {
    goto L610;
  }
  if (dls001_3.kflag <=  - 2)
  {
    rh = min(rh, .2);
  }
  /* ----------------------------------------------------------------------- */
  /* If there is a change of order, reset NQ, L, and the coefficients. */
  /* In any case H is reset according to RH and the YH array is rescaled. */
  /* Then exit from 690 if the step was OK, or redo the step otherwise. */
  /* ----------------------------------------------------------------------- */
  if (newq == dls001_3.nq)
  {
    goto L170;
  }
  L630: dls001_3.nq = newq;
  dls001_3.l = dls001_3.nq + 1;
  iret = 2;
  goto L150;
  /* ----------------------------------------------------------------------- */
  /* Control reaches this section if 3 or more failures have occured. */
  /* If 10 failures have occurred, exit with KFLAG = -1. */
  /* It is assumed that the derivatives that have accumulated in the */
  /* YH array have errors of the wrong order.  Hence the first */
  /* derivative is recomputed, and the order is set to 1.  Then */
  /* H is reduced by a factor of 10, and the step is retried, */
  /* until it succeeds or H reaches HMIN. */
  /* ----------------------------------------------------------------------- */
  L640: if (dls001_3.kflag ==  - 10)
  {
    goto L660;
  }
  rh = .1;
  /* Computing MAX */
  d__1 = dls001_3.hmin / abs(dls001_3.h__);
  rh = max(d__1, rh);
  dls001_3.h__ *= rh;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L645: */
    y[i__] = yh[i__ + yh_dim1];
  }
  (*f)(&neq[1], &dls001_3.tn, &y[1], &savf[1]);
  ++dls001_3.nfe;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L650: */
    yh[i__ + (yh_dim1 << 1)] = dls001_3.h__ * savf[i__];
  }
  dls001_3.ipup = dls001_3.miter;
  dls001_3.ialth = 5;
  if (dls001_3.nq == 1)
  {
    goto L200;
  }
  dls001_3.nq = 1;
  dls001_3.l = 2;
  iret = 3;
  goto L150;
  /* ----------------------------------------------------------------------- */
  /* All returns are made through this section.  H is saved in HOLD */
  /* to allow the caller to change H on the next step. */
  /* ----------------------------------------------------------------------- */
  L660: dls001_3.kflag =  - 1;
  goto L720;
  L670: dls001_3.kflag =  - 2;
  goto L720;
  L680: dls001_3.kflag =  - 3;
  goto L720;
  L690: dls001_3.rmax = 10.;
  L700: r__ = 1. / dls001_3.tesco[dls001_3.nqu *3-2];
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L710: */
    acor[i__] *= r__;
  }
  L720: dls001_3.hold = dls001_3.h__;
  dls001_3.jstart = 1;
  return 0;
  /* ----------------------- End of Subroutine DSTOKA ---------------------- */
} /* dstoka_ */

/* DECK DSETPK */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dsetpk_
   (integer *neq, doublereal *y, doublereal *ysv, doublereal *ewt, doublereal
   *ftem, doublereal *savf, integer *jok, doublereal *wm, integer *iwm, S_fp f,
   S_fp jac)
{
  static doublereal hl0;
  static integer ier;

  /* ----------------------------------------------------------------------- */
  /* DSETPK is called by DSTOKA to interface with the user-supplied */
  /* routine JAC, to compute and process relevant parts of */
  /* the matrix P = I - H*EL(1)*J , where J is the Jacobian df/dy, */
  /* as need for preconditioning matrix operations later. */

  /* In addition to variables described previously, communication */
  /* with DSETPK uses the following: */
  /* Y     = array containing predicted values on entry. */
  /* YSV   = array containing predicted y, to be saved (YH1 in DSTOKA). */
  /* FTEM  = work array of length N (ACOR in DSTOKA). */
  /* SAVF  = array containing f evaluated at predicted y. */
  /* JOK   = input flag showing whether it was judged that Jacobian matrix */
  /*         data need not be recomputed (JOK = 1) or needs to be */
  /*         (JOK = -1). */
  /* WM    = real work space for matrices. */
  /*         Space for preconditioning data starts at WM(LOCWP). */
  /* IWM   = integer work space. */
  /*         Space for preconditioning data starts at IWM(LOCIWP). */
  /* IERPJ = output error flag,  = 0 if no trouble, .gt. 0 if */
  /*         JAC returned an error flag. */
  /* JCUR  = output flag to indicate whether the matrix data involved */
  /*         is now current (JCUR = 1) or not (JCUR = 0). */
  /* This routine also uses Common variables EL0, H, TN, IERPJ, JCUR, NJE. */
  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --iwm;
  --wm;
  --savf;
  --ftem;
  --ewt;
  --ysv;
  --y;
  --neq;

  /* Function Body */
  dls001_1.ierpj = 0;
  dls001_1.jcur = 0;
  if (*jok ==  - 1)
  {
    dls001_1.jcur = 1;
  }
  hl0 = dls001_1.el0 * dls001_1.h__;
  (*jac)((S_fp)f, &neq[1], &dls001_1.tn, &y[1], &ysv[1], &ewt[1], &savf[1], 
    &ftem[1], &hl0, jok, &wm[dlpk01_1.locwp], &iwm[dlpk01_1.lociwp], &ier);
  ++dls001_1.nje;
  if (ier == 0)
  {
    return 0;
  }
  dls001_1.ierpj = 1;
  return 0;
  /* ----------------------- End of Subroutine DSETPK ---------------------- */
} /* dsetpk_ */

/* DECK DSRCKR */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dsrckr_
   (doublereal *rsav, integer *isav, integer *job)
{
  /* Initialized data */

  static integer lenrls = 218;
  static integer lenils = 37;
  static integer lenrlp = 4;
  static integer lenilp = 13;
  static integer lenrlr = 5;
  static integer lenilr = 9;

  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__, ioff;

  /* ----------------------------------------------------------------------- */
  /* This routine saves or restores (depending on JOB) the contents of */
  /* the Common blocks DLS001, DLS002, DLSR01, DLPK01, which */
  /* are used internally by the DLSODKR solver. */

  /* RSAV = real array of length 228 or more. */
  /* ISAV = integer array of length 63 or more. */
  /* JOB  = flag indicating to save or restore the Common blocks: */
  /*        JOB  = 1 if Common is to be saved (written to RSAV/ISAV) */
  /*        JOB  = 2 if Common is to be restored (read from RSAV/ISAV) */
  /*        A call with JOB = 2 presumes a prior call with JOB = 1. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --isav;
  --rsav;

  /* Function Body */

  if (*job == 2)
  {
    goto L100;
  }
  dcopy_(&lenrls, dls001_2.rls, &c__1, &rsav[1], &c__1);
  rsav[lenrls + 1] = dls002_2.rls2;
  dcopy_(&lenrlr, dlsr01_3.rlsr, &c__1, &rsav[lenrls + 2], &c__1);
  dcopy_(&lenrlp, dlpk01_2.rlsp, &c__1, &rsav[lenrls + lenrlr + 2], &c__1);
  i__1 = lenils;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L20: */
    isav[i__] = dls001_2.ils[i__ - 1];
  }
  isav[lenils + 1] = dls002_2.ils2[0];
  isav[lenils + 2] = dls002_2.ils2[1];
  isav[lenils + 3] = dls002_2.ils2[2];
  isav[lenils + 4] = dls002_2.ils2[3];
  ioff = lenils + 2;
  i__1 = lenilr;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L30: */
    isav[ioff + i__] = dlsr01_3.ilsr[i__ - 1];
  }
  ioff += lenilr;
  i__1 = lenilp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L40: */
    isav[ioff + i__] = dlpk01_2.ilsp[i__ - 1];
  }
  return 0;

  L100: dcopy_(&lenrls, &rsav[1], &c__1, dls001_2.rls, &c__1);
  dls002_2.rls2 = rsav[lenrls + 1];
  dcopy_(&lenrlr, &rsav[lenrls + 2], &c__1, dlsr01_3.rlsr, &c__1);
  dcopy_(&lenrlp, &rsav[lenrls + lenrlr + 2], &c__1, dlpk01_2.rlsp, &c__1);
  i__1 = lenils;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L120: */
    dls001_2.ils[i__ - 1] = isav[i__];
  }
  dls002_2.ils2[0] = isav[lenils + 1];
  dls002_2.ils2[1] = isav[lenils + 2];
  dls002_2.ils2[2] = isav[lenils + 3];
  dls002_2.ils2[3] = isav[lenils + 4];
  ioff = lenils + 2;
  i__1 = lenilr;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L130: */
    dlsr01_3.ilsr[i__ - 1] = isav[ioff + i__];
  }
  ioff += lenilr;
  i__1 = lenilp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L140: */
    dlpk01_2.ilsp[i__ - 1] = isav[ioff + i__];
  }
  return 0;
  /* ----------------------- End of Subroutine DSRCKR ---------------------- */
} /* dsrckr_ */

/* DECK DAINVG */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dainvg_
   (S_fp res, S_fp adda, integer *neq, doublereal *t, doublereal *y, doublereal
   *ydot, integer *miter, integer *ml, integer *mu, doublereal *pw, integer
   *ipvt, integer *ier)
{
  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__, mlp1;
  static integer lenpw, nrowpw;

  /* ----------------------------------------------------------------------- */
  /* This subroutine computes the initial value */
  /* of the vector YDOT satisfying */
  /*     A * YDOT = g(t,y) */
  /* when A is nonsingular.  It is called by DLSODI for */
  /* initialization only, when ISTATE = 0 . */
  /* DAINVG returns an error flag IER: */
  /*   IER  =  0  means DAINVG was successful. */
  /*   IER .ge. 2 means RES returned an error flag IRES = IER. */
  /*   IER .lt. 0 means the a-matrix was found to be singular. */
  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --ipvt;
  --pw;
  --ydot;
  --y;

  /* Function Body */
  if (*miter >= 4)
  {
    goto L100;
  }

  /* Full matrix case ----------------------------------------------------- */

  lenpw =  *neq **neq;
  i__1 = lenpw;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    pw[i__] = 0.;
  }

  *ier = 1;
  (*res)(neq, t, &y[1], &pw[1], &ydot[1], ier);
  if (*ier > 1)
  {
    return 0;
  }

  (*adda)(neq, t, &y[1], &c__0, &c__0, &pw[1], neq);
  dgefa_(&pw[1], neq, neq, &ipvt[1], ier);
  if (*ier == 0)
  {
    goto L20;
  }
  *ier =  - (*ier);
  return 0;
  L20: dgesl_(&pw[1], neq, neq, &ipvt[1], &ydot[1], &c__0);
  return 0;

  /* Band matrix case ----------------------------------------------------- */

  L100: nrowpw = (*ml << 1) +  *mu + 1;
  lenpw =  *neq * nrowpw;
  i__1 = lenpw;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L110: */
    pw[i__] = 0.;
  }

  *ier = 1;
  (*res)(neq, t, &y[1], &pw[1], &ydot[1], ier);
  if (*ier > 1)
  {
    return 0;
  }

  mlp1 =  *ml + 1;
  (*adda)(neq, t, &y[1], ml, mu, &pw[mlp1], &nrowpw);
  dgbfa_(&pw[1], &nrowpw, neq, ml, mu, &ipvt[1], ier);
  if (*ier == 0)
  {
    goto L120;
  }
  *ier =  - (*ier);
  return 0;
  L120: dgbsl_(&pw[1], &nrowpw, neq, ml, mu, &ipvt[1], &ydot[1], &c__0);
  return 0;
  /* ----------------------- End of Subroutine DAINVG ---------------------- */
} /* dainvg_ */

/* DECK DSTODI */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dstodi_
   (integer *neq, doublereal *y, doublereal *yh, integer *nyh, doublereal *yh1,
   doublereal *ewt, doublereal *savf, doublereal *savr, doublereal *acor,
   doublereal *wm, integer *iwm, S_fp res, U_fp adda, U_fp jac, S_fp pjac, S_fp
   slvs)
{
  /* System generated locals */
  integer yh_dim1, yh_offset, i__1, i__2;
  doublereal d__1, d__2, d__3;

  /* Local variables */
  static integer i__, j, m;
  static doublereal r__;
  static integer i1, jb;
  static doublereal rh, del, ddn;
  static integer ncf, kgo;
  static doublereal dsm, dup, el1h, dcon, delp, eljh, rhdn, exdn;
  static integer ires, iret;
  static doublereal rhsm;
  static integer newq;
  static doublereal exsm, rhup, exup, told;
  static integer iredo;

  /* ----------------------------------------------------------------------- */
  /* DSTODI performs one step of the integration of an initial value */
  /* problem for a system of Ordinary Differential Equations. */
  /* Note: DSTODI is independent of the value of the iteration method */
  /* indicator MITER, and hence is independent */
  /* of the type of chord method used, or the Jacobian structure. */
  /* Communication with DSTODI is done with the following variables: */

  /* NEQ    = integer array containing problem size in NEQ(1), and */
  /*          passed as the NEQ argument in all calls to RES, ADDA, */
  /*          and JAC. */
  /* Y      = an array of length .ge. N used as the Y argument in */
  /*          all calls to RES, JAC, and ADDA. */
  /* NEQ    = integer array containing problem size in NEQ(1), and */
  /*          passed as the NEQ argument in all calls tO RES, G, ADDA, */
  /*          and JAC. */
  /* YH     = an NYH by LMAX array containing the dependent variables */
  /*          and their approximate scaled derivatives, where */
  /*          LMAX = MAXORD + 1.  YH(i,j+1) contains the approximate */
  /*          j-th derivative of y(i), scaled by H**j/factorial(j) */
  /*          (j = 0,1,...,NQ).  On entry for the first step, the first */
  /*          two columns of YH must be set from the initial values. */
  /* NYH    = a constant integer .ge. N, the first dimension of YH. */
  /* YH1    = a one-dimensional array occupying the same space as YH. */
  /* EWT    = an array of length N containing multiplicative weights */
  /*          for local error measurements.  Local errors in y(i) are */
  /*          compared to 1.0/EWT(i) in various error tests. */
  /* SAVF   = an array of working storage, of length N. also used for */
  /*          input of YH(*,MAXORD+2) when JSTART = -1 and MAXORD is less */
  /*          than the current order NQ. */
  /*          Same as YDOTI in the driver. */
  /* SAVR   = an array of working storage, of length N. */
  /* ACOR   = a work array of length N used for the accumulated */
  /*          corrections. On a succesful return, ACOR(i) contains */
  /*          the estimated one-step local error in y(i). */
  /* WM,IWM = real and integer work arrays associated with matrix */
  /*          operations in chord iteration. */
  /* PJAC   = name of routine to evaluate and preprocess Jacobian matrix. */
  /* SLVS   = name of routine to solve linear system in chord iteration. */
  /* CCMAX  = maximum relative change in H*EL0 before PJAC is called. */
  /* H      = the step size to be attempted on the next step. */
  /*          H is altered by the error control algorithm during the */
  /*          problem.  H can be either positive or negative, but its */
  /*          sign must remain constant throughout the problem. */
  /* HMIN   = the minimum absolute value of the step size H to be used. */
  /* HMXI   = inverse of the maximum absolute value of H to be used. */
  /*          HMXI = 0.0 is allowed and corresponds to an infinite HMAX. */
  /*          HMIN and HMXI may be changed at any time, but will not */
  /*          take effect until the next change of H is considered. */
  /* TN     = the independent variable. TN is updated on each step taken. */
  /* JSTART = an integer used for input only, with the following */
  /*          values and meanings: */
  /*               0  perform the first step. */
  /*           .gt.0  take a new step continuing from the last. */
  /*              -1  take the next step with a new value of H, MAXORD, */
  /*                    N, METH, MITER, and/or matrix parameters. */
  /*              -2  take the next step with a new value of H, */
  /*                    but with other inputs unchanged. */
  /*          On return, JSTART is set to 1 to facilitate continuation. */
  /* KFLAG  = a completion code with the following meanings: */
  /*               0  the step was succesful. */
  /*              -1  the requested error could not be achieved. */
  /*              -2  corrector convergence could not be achieved. */
  /*              -3  RES ordered immediate return. */
  /*              -4  error condition from RES could not be avoided. */
  /*              -5  fatal error in PJAC or SLVS. */
  /*          A return with KFLAG = -1, -2, or -4 means either */
  /*          ABS(H) = HMIN or 10 consecutive failures occurred. */
  /*          On a return with KFLAG negative, the values of TN and */
  /*          the YH array are as of the beginning of the last */
  /*          step, and H is the last step size attempted. */
  /* MAXORD = the maximum order of integration method to be allowed. */
  /* MAXCOR = the maximum number of corrector iterations allowed. */
  /* MSBP   = maximum number of steps between PJAC calls. */
  /* MXNCF  = maximum number of convergence failures allowed. */
  /* METH/MITER = the method flags.  See description in driver. */
  /* N      = the number of first-order differential equations. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --neq;
  --y;
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --yh1;
  --ewt;
  --savf;
  --savr;
  --acor;
  --wm;
  --iwm;

  /* Function Body */
  dls001_3.kflag = 0;
  told = dls001_3.tn;
  ncf = 0;
  dls001_3.ierpj = 0;
  dls001_3.iersl = 0;
  dls001_3.jcur = 0;
  dls001_3.icf = 0;
  delp = 0.;
  if (dls001_3.jstart > 0)
  {
    goto L200;
  }
  if (dls001_3.jstart ==  - 1)
  {
    goto L100;
  }
  if (dls001_3.jstart ==  - 2)
  {
    goto L160;
  }
  /* ----------------------------------------------------------------------- */
  /* On the first call, the order is set to 1, and other variables are */
  /* initialized.  RMAX is the maximum ratio by which H can be increased */
  /* in a single step.  It is initially 1.E4 to compensate for the small */
  /* initial H, but then is normally equal to 10.  If a failure */
  /* occurs (in corrector convergence or error test), RMAX is set at 2 */
  /* for the next increase. */
  /* ----------------------------------------------------------------------- */
  dls001_3.lmax = dls001_3.maxord + 1;
  dls001_3.nq = 1;
  dls001_3.l = 2;
  dls001_3.ialth = 2;
  dls001_3.rmax = 1e4;
  dls001_3.rc = 0.;
  dls001_3.el0 = 1.;
  dls001_3.crate = .7;
  dls001_3.hold = dls001_3.h__;
  dls001_3.meo = dls001_3.meth;
  dls001_3.nslp = 0;
  dls001_3.ipup = dls001_3.miter;
  iret = 3;
  goto L140;
  /* ----------------------------------------------------------------------- */
  /* The following block handles preliminaries needed when JSTART = -1. */
  /* IPUP is set to MITER to force a matrix update. */
  /* If an order increase is about to be considered (IALTH = 1), */
  /* IALTH is reset to 2 to postpone consideration one more step. */
  /* If the caller has changed METH, DCFODE is called to reset */
  /* the coefficients of the method. */
  /* If the caller has changed MAXORD to a value less than the current */
  /* order NQ, NQ is reduced to MAXORD, and a new H chosen accordingly. */
  /* If H is to be changed, YH must be rescaled. */
  /* If H or METH is being changed, IALTH is reset to L = NQ + 1 */
  /* to prevent further changes in H for that many steps. */
  /* ----------------------------------------------------------------------- */
  L100: dls001_3.ipup = dls001_3.miter;
  dls001_3.lmax = dls001_3.maxord + 1;
  if (dls001_3.ialth == 1)
  {
    dls001_3.ialth = 2;
  }
  if (dls001_3.meth == dls001_3.meo)
  {
    goto L110;
  }
  dcfode_(&dls001_3.meth, dls001_3.elco, dls001_3.tesco);
  dls001_3.meo = dls001_3.meth;
  if (dls001_3.nq > dls001_3.maxord)
  {
    goto L120;
  }
  dls001_3.ialth = dls001_3.l;
  iret = 1;
  goto L150;
  L110: if (dls001_3.nq <= dls001_3.maxord)
  {
    goto L160;
  }
  L120: dls001_3.nq = dls001_3.maxord;
  dls001_3.l = dls001_3.lmax;
  i__1 = dls001_3.l;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L125: */
    dls001_3.el[i__ - 1] = dls001_3.elco[i__ + dls001_3.nq * 13-14];
  }
  dls001_3.nqnyh = dls001_3.nq **nyh;
  dls001_3.rc = dls001_3.rc * dls001_3.el[0] / dls001_3.el0;
  dls001_3.el0 = dls001_3.el[0];
  dls001_3.conit = .5 / (dls001_3.nq + 2);
  ddn = dvnorm_(&dls001_3.n, &savf[1], &ewt[1]) / dls001_3.tesco[dls001_3.l*3
    -3];
  exdn = 1. / dls001_3.l;
  rhdn = 1. / (pow_dd(&ddn, &exdn) *1.3 + 1.3e-6);
  rh = min(rhdn, 1.);
  iredo = 3;
  if (dls001_3.h__ == dls001_3.hold)
  {
    goto L170;
  }
  /* Computing MIN */
  d__2 = rh, d__3 = (d__1 = dls001_3.h__ / dls001_3.hold, abs(d__1));
  rh = min(d__2, d__3);
  dls001_3.h__ = dls001_3.hold;
  goto L175;
  /* ----------------------------------------------------------------------- */
  /* DCFODE is called to get all the integration coefficients for the */
  /* current METH.  Then the EL vector and related constants are reset */
  /* whenever the order NQ is changed, or at the start of the problem. */
  /* ----------------------------------------------------------------------- */
  L140: dcfode_(&dls001_3.meth, dls001_3.elco, dls001_3.tesco);
  L150: i__1 = dls001_3.l;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L155: */
    dls001_3.el[i__ - 1] = dls001_3.elco[i__ + dls001_3.nq * 13-14];
  }
  dls001_3.nqnyh = dls001_3.nq **nyh;
  dls001_3.rc = dls001_3.rc * dls001_3.el[0] / dls001_3.el0;
  dls001_3.el0 = dls001_3.el[0];
  dls001_3.conit = .5 / (dls001_3.nq + 2);
  switch (iret)
  {
    case 1:
      goto L160;
    case 2:
      goto L170;
    case 3:
      goto L200;
  }
  /* ----------------------------------------------------------------------- */
  /* If H is being changed, the H ratio RH is checked against */
  /* RMAX, HMIN, and HMXI, and the YH array rescaled.  IALTH is set to */
  /* L = NQ + 1 to prevent a change of H for that many steps, unless */
  /* forced by a convergence or error test failure. */
  /* ----------------------------------------------------------------------- */
  L160: if (dls001_3.h__ == dls001_3.hold)
  {
    goto L200;
  }
  rh = dls001_3.h__ / dls001_3.hold;
  dls001_3.h__ = dls001_3.hold;
  iredo = 3;
  goto L175;
  L170: 
  /* Computing MAX */
  d__1 = rh, d__2 = dls001_3.hmin / abs(dls001_3.h__);
  rh = max(d__1, d__2);
  L175: rh = min(rh, dls001_3.rmax);
  /* Computing MAX */
  d__1 = 1., d__2 = abs(dls001_3.h__) *dls001_3.hmxi * rh;
  rh /= max(d__1, d__2);
  r__ = 1.;
  i__1 = dls001_3.l;
  for (j = 2; j <= i__1; ++j)
  {
    r__ *= rh;
    i__2 = dls001_3.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L180: */
      yh[i__ + j * yh_dim1] *= r__;
    }
  }
  dls001_3.h__ *= rh;
  dls001_3.rc *= rh;
  dls001_3.ialth = dls001_3.l;
  if (iredo == 0)
  {
    goto L690;
  }
  /* ----------------------------------------------------------------------- */
  /* This section computes the predicted values by effectively */
  /* multiplying the YH array by the Pascal triangle matrix. */
  /* RC is the ratio of new to old values of the coefficient  H*EL(1). */
  /* When RC differs from 1 by more than CCMAX, IPUP is set to MITER */
  /* to force PJAC to be called. */
  /* In any case, PJAC is called at least every MSBP steps. */
  /* ----------------------------------------------------------------------- */
  L200: if ((d__1 = dls001_3.rc - 1., abs(d__1)) > dls001_3.ccmax)
  {
    dls001_3.ipup = dls001_3.miter;
  }
  if (dls001_3.nst >= dls001_3.nslp + dls001_3.msbp)
  {
    dls001_3.ipup = dls001_3.miter;
  }
  dls001_3.tn += dls001_3.h__;
  i1 = dls001_3.nqnyh + 1;
  i__2 = dls001_3.nq;
  for (jb = 1; jb <= i__2; ++jb)
  {
    i1 -=  *nyh;
    /* DIR$ IVDEP */
    i__1 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__1; ++i__)
    {
      /* L210: */
      yh1[i__] += yh1[i__ +  *nyh];
    }
    /* L215: */
  }
  /* ----------------------------------------------------------------------- */
  /* Up to MAXCOR corrector iterations are taken.  A convergence test is */
  /* made on the RMS-norm of each correction, weighted by H and the */
  /* error weight vector EWT.  The sum of the corrections is accumulated */
  /* in ACOR(i).  The YH array is not altered in the corrector loop. */
  /* ----------------------------------------------------------------------- */
  L220: m = 0;
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    savf[i__] = yh[i__ + (yh_dim1 << 1)] / dls001_3.h__;
    /* L230: */
    y[i__] = yh[i__ + yh_dim1];
  }
  if (dls001_3.ipup <= 0)
  {
    goto L240;
  }
  /* ----------------------------------------------------------------------- */
  /* If indicated, the matrix P = A - H*EL(1)*dr/dy is reevaluated and */
  /* preprocessed before starting the corrector iteration.  IPUP is set */
  /* to 0 as an indicator that this has been done. */
  /* ----------------------------------------------------------------------- */
  (*pjac)(&neq[1], &y[1], &yh[yh_offset], nyh, &ewt[1], &acor[1], &savr[1], 
    &savf[1], &wm[1], &iwm[1], (U_fp)res, (U_fp)jac, (U_fp)adda);
  dls001_3.ipup = 0;
  dls001_3.rc = 1.;
  dls001_3.nslp = dls001_3.nst;
  dls001_3.crate = .7;
  if (dls001_3.ierpj == 0)
  {
    goto L250;
  }
  if (dls001_3.ierpj < 0)
  {
    goto L435;
  }
  ires = dls001_3.ierpj;
  switch (ires)
  {
    case 1:
      goto L430;
    case 2:
      goto L435;
    case 3:
      goto L430;
  }
  /* Get residual at predicted values, if not already done in PJAC. ------- */
  L240: ires = 1;
  (*res)(&neq[1], &dls001_3.tn, &y[1], &savf[1], &savr[1], &ires);
  ++dls001_3.nfe;
  kgo = abs(ires);
  switch (kgo)
  {
    case 1:
      goto L250;
    case 2:
      goto L435;
    case 3:
      goto L430;
  }
  L250: i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    /* L260: */
    acor[i__] = 0.;
  }
  /* ----------------------------------------------------------------------- */
  /* Solve the linear system with the current residual as */
  /* right-hand side and P as coefficient matrix. */
  /* ----------------------------------------------------------------------- */
  L270: (*slvs)(&wm[1], &iwm[1], &savr[1], &savf[1]);
  if (dls001_3.iersl < 0)
  {
    goto L430;
  }
  if (dls001_3.iersl > 0)
  {
    goto L410;
  }
  el1h = dls001_3.el[0] *dls001_3.h__;
  del = dvnorm_(&dls001_3.n, &savr[1], &ewt[1]) *abs(dls001_3.h__);
  i__2 = dls001_3.n;
  for (i__ = 1; i__ <= i__2; ++i__)
  {
    acor[i__] += savr[i__];
    savf[i__] = acor[i__] + yh[i__ + (yh_dim1 << 1)] / dls001_3.h__;
    /* L380: */
    y[i__] = yh[i__ + yh_dim1] + el1h * acor[i__];
  }
  /* ----------------------------------------------------------------------- */
  /* Test for convergence.  If M .gt. 0, an estimate of the convergence */
  /* rate constant is stored in CRATE, and this is used in the test. */
  /* ----------------------------------------------------------------------- */
  if (m != 0)
  {
    /* Computing MAX */
    d__1 = dls001_3.crate * .2, d__2 = del / delp;
    dls001_3.crate = max(d__1, d__2);
  }
  /* Computing MIN */
  d__1 = 1., d__2 = dls001_3.crate * 1.5;
  dcon = del * min(d__1, d__2) / (dls001_3.tesco[dls001_3.nq *3-2]
    *dls001_3.conit);
  if (dcon <= 1.)
  {
    goto L460;
  }
  ++m;
  if (m == dls001_3.maxcor)
  {
    goto L410;
  }
  if (m >= 2 && del > delp *2.)
  {
    goto L410;
  }
  delp = del;
  ires = 1;
  (*res)(&neq[1], &dls001_3.tn, &y[1], &savf[1], &savr[1], &ires);
  ++dls001_3.nfe;
  kgo = abs(ires);
  switch (kgo)
  {
    case 1:
      goto L270;
    case 2:
      goto L435;
    case 3:
      goto L410;
  }
  /* ----------------------------------------------------------------------- */
  /* The correctors failed to converge, or RES has returned abnormally. */
  /* on a convergence failure, if the Jacobian is out of date, PJAC is */
  /* called for the next try.  Otherwise the YH array is retracted to its */
  /* values before prediction, and H is reduced, if possible. */
  /* take an error exit if IRES = 2, or H cannot be reduced, or MXNCF */
  /* failures have occurred, or a fatal error occurred in PJAC or SLVS. */
  /* ----------------------------------------------------------------------- */
  L410: dls001_3.icf = 1;
  if (dls001_3.jcur == 1)
  {
    goto L430;
  }
  dls001_3.ipup = dls001_3.miter;
  goto L220;
  L430: dls001_3.icf = 2;
  ++ncf;
  dls001_3.rmax = 2.;
  L435: dls001_3.tn = told;
  i1 = dls001_3.nqnyh + 1;
  i__2 = dls001_3.nq;
  for (jb = 1; jb <= i__2; ++jb)
  {
    i1 -=  *nyh;
    /* DIR$ IVDEP */
    i__1 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__1; ++i__)
    {
      /* L440: */
      yh1[i__] -= yh1[i__ +  *nyh];
    }
    /* L445: */
  }
  if (ires == 2)
  {
    goto L680;
  }
  if (dls001_3.ierpj < 0 || dls001_3.iersl < 0)
  {
    goto L685;
  }
  if (abs(dls001_3.h__) <= dls001_3.hmin *1.00001)
  {
    goto L450;
  }
  if (ncf == dls001_3.mxncf)
  {
    goto L450;
  }
  rh = .25;
  dls001_3.ipup = dls001_3.miter;
  iredo = 1;
  goto L170;
  L450: if (ires == 3)
  {
    goto L680;
  }
  goto L670;
  /* ----------------------------------------------------------------------- */
  /* The corrector has converged.  JCUR is set to 0 */
  /* to signal that the Jacobian involved may need updating later. */
  /* The local error test is made and control passes to statement 500 */
  /* if it fails. */
  /* ----------------------------------------------------------------------- */
  L460: dls001_3.jcur = 0;
  if (m == 0)
  {
    dsm = del / dls001_3.tesco[dls001_3.nq *3-2];
  }
  if (m > 0)
  {
    dsm = abs(dls001_3.h__) *dvnorm_(&dls001_3.n, &acor[1], &ewt[1]) /
      dls001_3.tesco[dls001_3.nq *3-2];
  }
  if (dsm > 1.)
  {
    goto L500;
  }
  /* ----------------------------------------------------------------------- */
  /* After a successful step, update the YH array. */
  /* Consider changing H if IALTH = 1.  Otherwise decrease IALTH by 1. */
  /* If IALTH is then 1 and NQ .lt. MAXORD, then ACOR is saved for */
  /* use in a possible order increase on the next step. */
  /* If a change in H is considered, an increase or decrease in order */
  /* by one is considered also.  A change in H is made only if it is by a */
  /* factor of at least 1.1.  If not, IALTH is set to 3 to prevent */
  /* testing for that many steps. */
  /* ----------------------------------------------------------------------- */
  dls001_3.kflag = 0;
  iredo = 0;
  ++dls001_3.nst;
  dls001_3.hu = dls001_3.h__;
  dls001_3.nqu = dls001_3.nq;
  i__2 = dls001_3.l;
  for (j = 1; j <= i__2; ++j)
  {
    eljh = dls001_3.el[j - 1] *dls001_3.h__;
    i__1 = dls001_3.n;
    for (i__ = 1; i__ <= i__1; ++i__)
    {
      /* L470: */
      yh[i__ + j * yh_dim1] += eljh * acor[i__];
    }
  }
  --dls001_3.ialth;
  if (dls001_3.ialth == 0)
  {
    goto L520;
  }
  if (dls001_3.ialth > 1)
  {
    goto L700;
  }
  if (dls001_3.l == dls001_3.lmax)
  {
    goto L700;
  }
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L490: */
    yh[i__ + dls001_3.lmax * yh_dim1] = acor[i__];
  }
  goto L700;
  /* ----------------------------------------------------------------------- */
  /* The error test failed.  KFLAG keeps track of multiple failures. */
  /* restore TN and the YH array to their previous values, and prepare */
  /* to try the step again.  Compute the optimum step size for this or */
  /* one lower order.  After 2 or more failures, H is forced to decrease */
  /* by a factor of 0.1 or less. */
  /* ----------------------------------------------------------------------- */
  L500: --dls001_3.kflag;
  dls001_3.tn = told;
  i1 = dls001_3.nqnyh + 1;
  i__1 = dls001_3.nq;
  for (jb = 1; jb <= i__1; ++jb)
  {
    i1 -=  *nyh;
    /* DIR$ IVDEP */
    i__2 = dls001_3.nqnyh;
    for (i__ = i1; i__ <= i__2; ++i__)
    {
      /* L510: */
      yh1[i__] -= yh1[i__ +  *nyh];
    }
    /* L515: */
  }
  dls001_3.rmax = 2.;
  if (abs(dls001_3.h__) <= dls001_3.hmin *1.00001)
  {
    goto L660;
  }
  if (dls001_3.kflag <=  - 7)
  {
    goto L660;
  }
  iredo = 2;
  rhup = 0.;
  goto L540;
  /* ----------------------------------------------------------------------- */
  /* Regardless of the success or failure of the step, factors */
  /* RHDN, RHSM, and RHUP are computed, by which H could be multiplied */
  /* at order NQ - 1, order NQ, or order NQ + 1, respectively. */
  /* In the case of failure, RHUP = 0.0 to avoid an order increase. */
  /* The largest of these is determined and the new order chosen */
  /* accordingly.  If the order is to be increased, we compute one */
  /* additional scaled derivative. */
  /* ----------------------------------------------------------------------- */
  L520: rhup = 0.;
  if (dls001_3.l == dls001_3.lmax)
  {
    goto L540;
  }
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L530: */
    savf[i__] = acor[i__] - yh[i__ + dls001_3.lmax * yh_dim1];
  }
  dup = abs(dls001_3.h__) *dvnorm_(&dls001_3.n, &savf[1], &ewt[1]) /
    dls001_3.tesco[dls001_3.nq *3-1];
  exup = 1. / (dls001_3.l + 1);
  rhup = 1. / (pow_dd(&dup, &exup) *1.4 + 1.4e-6);
  L540: exsm = 1. / dls001_3.l;
  rhsm = 1. / (pow_dd(&dsm, &exsm) *1.2 + 1.2e-6);
  rhdn = 0.;
  if (dls001_3.nq == 1)
  {
    goto L560;
  }
  ddn = dvnorm_(&dls001_3.n, &yh[dls001_3.l *yh_dim1 + 1], &ewt[1]) /
    dls001_3.tesco[dls001_3.nq *3-3];
  exdn = 1. / dls001_3.nq;
  rhdn = 1. / (pow_dd(&ddn, &exdn) *1.3 + 1.3e-6);
  L560: if (rhsm >= rhup)
  {
    goto L570;
  }
  if (rhup > rhdn)
  {
    goto L590;
  }
  goto L580;
  L570: if (rhsm < rhdn)
  {
    goto L580;
  }
  newq = dls001_3.nq;
  rh = rhsm;
  goto L620;
  L580: newq = dls001_3.nq - 1;
  rh = rhdn;
  if (dls001_3.kflag < 0 && rh > 1.)
  {
    rh = 1.;
  }
  goto L620;
  L590: newq = dls001_3.l;
  rh = rhup;
  if (rh < 1.1)
  {
    goto L610;
  }
  r__ = dls001_3.h__ * dls001_3.el[dls001_3.l - 1] / dls001_3.l;
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L600: */
    yh[i__ + (newq + 1) *yh_dim1] = acor[i__] *r__;
  }
  goto L630;
  L610: dls001_3.ialth = 3;
  goto L700;
  L620: if (dls001_3.kflag == 0 && rh < 1.1)
  {
    goto L610;
  }
  if (dls001_3.kflag <=  - 2)
  {
    rh = min(rh, .1);
  }
  /* ----------------------------------------------------------------------- */
  /* If there is a change of order, reset NQ, L, and the coefficients. */
  /* In any case H is reset according to RH and the YH array is rescaled. */
  /* Then exit from 690 if the step was OK, or redo the step otherwise. */
  /* ----------------------------------------------------------------------- */
  if (newq == dls001_3.nq)
  {
    goto L170;
  }
  L630: dls001_3.nq = newq;
  dls001_3.l = dls001_3.nq + 1;
  iret = 2;
  goto L150;
  /* ----------------------------------------------------------------------- */
  /* All returns are made through this section.  H is saved in HOLD */
  /* to allow the caller to change H on the next step. */
  /* ----------------------------------------------------------------------- */
  L660: dls001_3.kflag =  - 1;
  goto L720;
  L670: dls001_3.kflag =  - 2;
  goto L720;
  L680: dls001_3.kflag =  - 1-ires;
  goto L720;
  L685: dls001_3.kflag =  - 5;
  goto L720;
  L690: dls001_3.rmax = 10.;
  L700: r__ = dls001_3.h__ / dls001_3.tesco[dls001_3.nqu *3-2];
  i__1 = dls001_3.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L710: */
    acor[i__] *= r__;
  }
  L720: dls001_3.hold = dls001_3.h__;
  dls001_3.jstart = 1;
  return 0;
  /* ----------------------- End of Subroutine DSTODI ---------------------- */
} /* dstodi_ */

/* DECK DPREPJI */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider
   ::dprepji_(integer *neq, doublereal *y, doublereal *yh, integer *nyh,
   doublereal *ewt, doublereal *rtem, doublereal *savr, doublereal *s,
   doublereal *wm, integer *iwm, S_fp res, S_fp jac, S_fp adda)
{
  /* System generated locals */
  integer yh_dim1, yh_offset, i__1, i__2, i__3, i__4;
  doublereal d__1, d__2;

  /* Local variables */
  static integer i__, j;
  static doublereal r__;
  static integer i1, i2, j1, ii, jj, ml, mu;
  static doublereal yi, yj, hl0;
  static integer ml3;
  static doublereal fac;
  static integer mba, ier;
  static doublereal con, yjj;
  static integer meb1, lenp, ires;
  static doublereal srur;
  static integer mband, meband;

  /* ----------------------------------------------------------------------- */
  /* DPREPJI is called by DSTODI to compute and process the matrix */
  /* P = A - H*EL(1)*J , where J is an approximation to the Jacobian dr/dy, */
  /* where r = g(t,y) - A(t,y)*s.  Here J is computed by the user-supplied */
  /* routine JAC if MITER = 1 or 4, or by finite differencing if MITER = */
  /* 2 or 5.  J is stored in WM, rescaled, and ADDA is called to generate */
  /* P. P is then subjected to LU decomposition in preparation */
  /* for later solution of linear systems with P as coefficient */
  /* matrix.  This is done by DGEFA if MITER = 1 or 2, and by */
  /* DGBFA if MITER = 4 or 5. */

  /* In addition to variables described previously, communication */
  /* with DPREPJI uses the following: */
  /* Y     = array containing predicted values on entry. */
  /* RTEM  = work array of length N (ACOR in DSTODI). */
  /* SAVR  = array used for output only.  On output it contains the */
  /*         residual evaluated at current values of t and y. */
  /* S     = array containing predicted values of dy/dt (SAVF in DSTODI). */
  /* WM    = real work space for matrices.  On output it contains the */
  /*         LU decomposition of P. */
  /*         Storage of matrix elements starts at WM(3). */
  /*         WM also contains the following matrix-related data: */
  /*         WM(1) = SQRT(UROUND), used in numerical Jacobian increments. */
  /* IWM   = integer work space containing pivot information, starting at */
  /*         IWM(21).  IWM also contains the band parameters */
  /*         ML = IWM(1) and MU = IWM(2) if MITER is 4 or 5. */
  /* EL0   = el(1) (input). */
  /* IERPJ = output error flag. */
  /*         = 0 if no trouble occurred, */
  /*         = 1 if the P matrix was found to be singular, */
  /*         = IRES (= 2 or 3) if RES returned IRES = 2 or 3. */
  /* JCUR  = output flag = 1 to indicate that the Jacobian matrix */
  /*         (or approximation) is now current. */
  /* This routine also uses the Common variables EL0, H, TN, UROUND, */
  /* MITER, N, NFE, and NJE. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --neq;
  --y;
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --ewt;
  --rtem;
  --savr;
  --s;
  --wm;
  --iwm;

  /* Function Body */
  ++dls001_1.nje;
  hl0 = dls001_1.h__ * dls001_1.el0;
  dls001_1.ierpj = 0;
  dls001_1.jcur = 1;
  switch (dls001_1.miter)
  {
    case 1:
      goto L100;
    case 2:
      goto L200;
    case 3:
      goto L300;
    case 4:
      goto L400;
    case 5:
      goto L500;
  }
  /* If MITER = 1, call RES, then JAC, and multiply by scalar. ------------ */
  L100: ires = 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ires);
  ++dls001_1.nfe;
  if (ires > 1)
  {
    goto L600;
  }
  lenp = dls001_1.n * dls001_1.n;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L110: */
    wm[i__ + 2] = 0.;
  }
  (*jac)(&neq[1], &dls001_1.tn, &y[1], &s[1], &c__0, &c__0, &wm[3], &dls001_1.n)
    ;
  con =  - hl0;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L120: */
    wm[i__ + 2] *= con;
  }
  goto L240;
  /* If MITER = 2, make N + 1 calls to RES to approximate J. -------------- */
  L200: ires =  - 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ires);
  ++dls001_1.nfe;
  if (ires > 1)
  {
    goto L600;
  }
  srur = wm[1];
  j1 = 2;
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    yj = y[j];
    /* Computing MAX */
    d__1 = srur * abs(yj), d__2 = .01 / ewt[j];
    r__ = max(d__1, d__2);
    y[j] += r__;
    fac =  - hl0 / r__;
    (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &rtem[1], &ires);
    ++dls001_1.nfe;
    if (ires > 1)
    {
      goto L600;
    }
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L220: */
      wm[i__ + j1] = (rtem[i__] - savr[i__]) *fac;
    }
    y[j] = yj;
    j1 += dls001_1.n;
    /* L230: */
  }
  ires = 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ires);
  ++dls001_1.nfe;
  if (ires > 1)
  {
    goto L600;
  }
  /* Add matrix A. -------------------------------------------------------- */
  L240: (*adda)(&neq[1], &dls001_1.tn, &y[1], &c__0, &c__0, &wm[3], &dls001_1.n)
    ;
  /* Do LU decomposition on P. -------------------------------------------- */
  dgefa_(&wm[3], &dls001_1.n, &dls001_1.n, &iwm[21], &ier);
  if (ier != 0)
  {
    dls001_1.ierpj = 1;
  }
  return 0;
  /* Dummy section for MITER = 3 */
  L300: return 0;
  /* If MITER = 4, call RES, then JAC, and multiply by scalar. ------------ */
  L400: ires = 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ires);
  ++dls001_1.nfe;
  if (ires > 1)
  {
    goto L600;
  }
  ml = iwm[1];
  mu = iwm[2];
  ml3 = ml + 3;
  mband = ml + mu + 1;
  meband = mband + ml;
  lenp = meband * dls001_1.n;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L410: */
    wm[i__ + 2] = 0.;
  }
  (*jac)(&neq[1], &dls001_1.tn, &y[1], &s[1], &ml, &mu, &wm[ml3], &meband);
  con =  - hl0;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L420: */
    wm[i__ + 2] *= con;
  }
  goto L570;
  /* If MITER = 5, make ML + MU + 2 calls to RES to approximate J. -------- */
  L500: ires =  - 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ires);
  ++dls001_1.nfe;
  if (ires > 1)
  {
    goto L600;
  }
  ml = iwm[1];
  mu = iwm[2];
  ml3 = ml + 3;
  mband = ml + mu + 1;
  mba = min(mband, dls001_1.n);
  meband = mband + ml;
  meb1 = meband - 1;
  srur = wm[1];
  i__1 = mba;
  for (j = 1; j <= i__1; ++j)
  {
    i__2 = dls001_1.n;
    i__3 = mband;
    for (i__ = j; i__3 < 0 ? i__ >= i__2: i__ <= i__2; i__ += i__3)
    {
      yi = y[i__];
      /* Computing MAX */
      d__1 = srur * abs(yi), d__2 = .01 / ewt[i__];
      r__ = max(d__1, d__2);
      /* L530: */
      y[i__] += r__;
    }
    (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &rtem[1], &ires);
    ++dls001_1.nfe;
    if (ires > 1)
    {
      goto L600;
    }
    i__3 = dls001_1.n;
    i__2 = mband;
    for (jj = j; i__2 < 0 ? jj >= i__3: jj <= i__3; jj += i__2)
    {
      y[jj] = yh[jj + yh_dim1];
      yjj = y[jj];
      /* Computing MAX */
      d__1 = srur * abs(yjj), d__2 = .01 / ewt[jj];
      r__ = max(d__1, d__2);
      fac =  - hl0 / r__;
      /* Computing MAX */
      i__4 = jj - mu;
      i1 = max(i__4, 1);
      /* Computing MIN */
      i__4 = jj + ml;
      i2 = min(i__4, dls001_1.n);
      ii = jj * meb1 - ml + 2;
      i__4 = i2;
      for (i__ = i1; i__ <= i__4; ++i__)
      {
        /* L540: */
        wm[ii + i__] = (rtem[i__] - savr[i__]) *fac;
      }
      /* L550: */
    }
    /* L560: */
  }
  ires = 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ires);
  ++dls001_1.nfe;
  if (ires > 1)
  {
    goto L600;
  }
  /* Add matrix A. -------------------------------------------------------- */
  L570: (*adda)(&neq[1], &dls001_1.tn, &y[1], &ml, &mu, &wm[ml3], &meband);
  /* Do LU decomposition of P. -------------------------------------------- */
  dgbfa_(&wm[3], &meband, &dls001_1.n, &ml, &mu, &iwm[21], &ier);
  if (ier != 0)
  {
    dls001_1.ierpj = 1;
  }
  return 0;
  /* Error return for IRES = 2 or IRES = 3 return from RES. --------------- */
  L600: dls001_1.ierpj = ires;
  return 0;
  /* ----------------------- End of Subroutine DPREPJI --------------------- */
} /* dprepji_ */

/* DECK DAIGBT */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::daigbt_
   (S_fp res, S_fp adda, integer *neq, doublereal *t, doublereal *y, doublereal
   *ydot, integer *mb, integer *nb, doublereal *pw, integer *ipvt, integer *ier)
{
  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__, lpb, lpc, lblox, lenpw;

  /* ----------------------------------------------------------------------- */
  /* This subroutine computes the initial value */
  /* of the vector YDOT satisfying */
  /*     A * YDOT = g(t,y) */
  /* when A is nonsingular.  It is called by DLSOIBT for */
  /* initialization only, when ISTATE = 0 . */
  /* DAIGBT returns an error flag IER: */
  /*   IER  =  0  means DAIGBT was successful. */
  /*   IER .ge. 2 means RES returned an error flag IRES = IER. */
  /*   IER .lt. 0 means the A matrix was found to have a singular */
  /*              diagonal block (hence YDOT could not be solved for). */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --ipvt;
  --pw;
  --ydot;
  --y;
  --neq;

  /* Function Body */
  lblox =  *mb **mb **nb;
  lpb = lblox + 1;
  lpc = lpb + lblox;
  lenpw = lblox * 3;
  i__1 = lenpw;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    pw[i__] = 0.;
  }
  *ier = 1;
  (*res)(&neq[1], t, &y[1], &pw[1], &ydot[1], ier);
  if (*ier > 1)
  {
    return 0;
  }
  (*adda)(&neq[1], t, &y[1], mb, nb, &pw[1], &pw[lpb], &pw[lpc]);
  ddecbt_(mb, nb, &pw[1], &pw[lpb], &pw[lpc], &ipvt[1], ier);
  if (*ier == 0)
  {
    goto L20;
  }
  *ier =  - (*ier);
  return 0;
  L20: dsolbt_(mb, nb, &pw[1], &pw[lpb], &pw[lpc], &ydot[1], &ipvt[1]);
  return 0;
  /* ----------------------- End of Subroutine DAIGBT ---------------------- */
} /* daigbt_ */

/* DECK DPJIBT */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dpjibt_
   (integer *neq, doublereal *y, doublereal *yh, integer *nyh, doublereal *ewt,
   doublereal *rtem, doublereal *savr, doublereal *s, doublereal *wm, integer
   *iwm, S_fp res, S_fp jac, S_fp adda)
{
  /* System generated locals */
  integer yh_dim1, yh_offset, i__1, i__2, i__3, i__4;
  doublereal d__1, d__2, d__3;

  /* Local variables */
  static integer i__, j, k;
  static doublereal r__;
  static integer j1, j2, k1, mb, nb;
  static doublereal hl0, fac;
  static integer iia, iib, iic, ipa, ipb, ipc, lpb, lpc, ier;
  static doublereal con;
  static integer lenp, mwid, ires, mbsq;
  static doublereal srur;
  static integer lblox;

  /* ----------------------------------------------------------------------- */
  /* DPJIBT is called by DSTODI to compute and process the matrix */
  /* P = A - H*EL(1)*J , where J is an approximation to the Jacobian dr/dy, */
  /* and r = g(t,y) - A(t,y)*s.  Here J is computed by the user-supplied */
  /* routine JAC if MITER = 1, or by finite differencing if MITER = 2. */
  /* J is stored in WM, rescaled, and ADDA is called to generate P. */
  /* P is then subjected to LU decomposition by DDECBT in preparation */
  /* for later solution of linear systems with P as coefficient matrix. */

  /* In addition to variables described previously, communication */
  /* with DPJIBT uses the following: */
  /* Y     = array containing predicted values on entry. */
  /* RTEM  = work array of length N (ACOR in DSTODI). */
  /* SAVR  = array used for output only.  On output it contains the */
  /*         residual evaluated at current values of t and y. */
  /* S     = array containing predicted values of dy/dt (SAVF in DSTODI). */
  /* WM    = real work space for matrices.  On output it contains the */
  /*         LU decomposition of P. */
  /*         Storage of matrix elements starts at WM(3). */
  /*         WM also contains the following matrix-related data: */
  /*         WM(1) = SQRT(UROUND), used in numerical Jacobian increments. */
  /* IWM   = integer work space containing pivot information, starting at */
  /*         IWM(21).  IWM also contains block structure parameters */
  /*         MB = IWM(1) and NB = IWM(2). */
  /* EL0   = EL(1) (input). */
  /* IERPJ = output error flag. */
  /*         = 0 if no trouble occurred, */
  /*         = 1 if the P matrix was found to be unfactorable, */
  /*         = IRES (= 2 or 3) if RES returned IRES = 2 or 3. */
  /* JCUR  = output flag = 1 to indicate that the Jacobian matrix */
  /*         (or approximation) is now current. */
  /* This routine also uses the Common variables EL0, H, TN, UROUND, */
  /* MITER, N, NFE, and NJE. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --neq;
  --y;
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --ewt;
  --rtem;
  --savr;
  --s;
  --wm;
  --iwm;

  /* Function Body */
  ++dls001_1.nje;
  hl0 = dls001_1.h__ * dls001_1.el0;
  dls001_1.ierpj = 0;
  dls001_1.jcur = 1;
  mb = iwm[1];
  nb = iwm[2];
  mbsq = mb * mb;
  lblox = mbsq * nb;
  lpb = lblox + 3;
  lpc = lpb + lblox;
  lenp = lblox * 3;
  switch (dls001_1.miter)
  {
    case 1:
      goto L100;
    case 2:
      goto L200;
  }
  /* If MITER = 1, call RES, then JAC, and multiply by scalar. ------------ */
  L100: ires = 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ires);
  ++dls001_1.nfe;
  if (ires > 1)
  {
    goto L600;
  }
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L110: */
    wm[i__ + 2] = 0.;
  }
  (*jac)(&neq[1], &dls001_1.tn, &y[1], &s[1], &mb, &nb, &wm[3], &wm[lpb],
    &wm[lpc]);
  con =  - hl0;
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L120: */
    wm[i__ + 2] *= con;
  }
  goto L260;

  /* If MITER = 2, make 3*MB + 1 calls to RES to approximate J. ----------- */
  L200: ires =  - 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ires);
  ++dls001_1.nfe;
  if (ires > 1)
  {
    goto L600;
  }
  mwid = mb * 3;
  srur = wm[1];
  i__1 = lenp;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L205: */
    wm[i__ + 2] = 0.;
  }
  for (k = 1; k <= 3; ++k)
  {
    i__1 = mb;
    for (j = 1; j <= i__1; ++j)
    {
      /*         Increment Y(I) for group of column indices, and call RES. ---- */
      j1 = j + (k - 1) *mb;
      i__2 = dls001_1.n;
      i__3 = mwid;
      for (i__ = j1; i__3 < 0 ? i__ >= i__2: i__ <= i__2; i__ += i__3)
      {
        /* Computing MAX */
        d__2 = srur *(d__1 = y[i__], abs(d__1)), d__3 = .01 / ewt[i__];
        r__ = max(d__2, d__3);
        y[i__] += r__;
        /* L210: */
      }
      (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &rtem[1], &ires);
      ++dls001_1.nfe;
      if (ires > 1)
      {
        goto L600;
      }
      i__3 = dls001_1.n;
      for (i__ = 1; i__ <= i__3; ++i__)
      {
        /* L215: */
        rtem[i__] -= savr[i__];
      }
      k1 = k;
      i__3 = dls001_1.n;
      i__2 = mwid;
      for (i__ = j1; i__2 < 0 ? i__ >= i__3: i__ <= i__3; i__ += i__2)
      {
        /*           Get Jacobian elements in column I (block-column K1). ------- */
        y[i__] = yh[i__ + yh_dim1];
        /* Computing MAX */
        d__2 = srur *(d__1 = y[i__], abs(d__1)), d__3 = .01 / ewt[i__];
        r__ = max(d__2, d__3);
        fac =  - hl0 / r__;
        /*           Compute and load elements PA(*,J,K1). ---------------------- */
        iia = i__ - j;
        ipa = (j - 1) *mb + 2+(k1 - 1) *mbsq;
        i__4 = mb;
        for (j2 = 1; j2 <= i__4; ++j2)
        {
          /* L221: */
          wm[ipa + j2] = rtem[iia + j2] *fac;
        }
        if (k1 <= 1)
        {
          goto L223;
        }
        /*           Compute and load elements PB(*,J,K1-1). -------------------- */
        iib = iia - mb;
        ipb = ipa + lblox - mbsq;
        i__4 = mb;
        for (j2 = 1; j2 <= i__4; ++j2)
        {
          /* L222: */
          wm[ipb + j2] = rtem[iib + j2] *fac;
        }
        L223: if (k1 >= nb)
        {
          goto L225;
        }
        /*           Compute and load elements PC(*,J,K1+1). -------------------- */
        iic = iia + mb;
        ipc = ipa + (lblox << 1) + mbsq;
        i__4 = mb;
        for (j2 = 1; j2 <= i__4; ++j2)
        {
          /* L224: */
          wm[ipc + j2] = rtem[iic + j2] *fac;
        }
        L225: if (k1 != 3)
        {
          goto L227;
        }
        /*           Compute and load elements PC(*,J,1). ----------------------- */
        ipc = ipa - (mbsq << 1) + (lblox << 1);
        i__4 = mb;
        for (j2 = 1; j2 <= i__4; ++j2)
        {
          /* L226: */
          wm[ipc + j2] = rtem[j2] *fac;
        }
        L227: if (k1 != nb - 2)
        {
          goto L229;
        }
        /*           Compute and load elements PB(*,J,NB). ---------------------- */
        iib = dls001_1.n - mb;
        ipb = ipa + (mbsq << 1) + lblox;
        i__4 = mb;
        for (j2 = 1; j2 <= i__4; ++j2)
        {
          /* L228: */
          wm[ipb + j2] = rtem[iib + j2] *fac;
        }
        L229: k1 += 3;
        /* L230: */
      }
      /* L240: */
    }
    /* L250: */
  }
  /* RES call for first corrector iteration. ------------------------------ */
  ires = 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ires);
  ++dls001_1.nfe;
  if (ires > 1)
  {
    goto L600;
  }
  /* Add matrix A. -------------------------------------------------------- */
  L260: (*adda)(&neq[1], &dls001_1.tn, &y[1], &mb, &nb, &wm[3], &wm[lpb],
    &wm[lpc]);
  /* Do LU decomposition on P. -------------------------------------------- */
  ddecbt_(&mb, &nb, &wm[3], &wm[lpb], &wm[lpc], &iwm[21], &ier);
  if (ier != 0)
  {
    dls001_1.ierpj = 1;
  }
  return 0;
  /* Error return for IRES = 2 or IRES = 3 return from RES. --------------- */
  L600: dls001_1.ierpj = ires;
  return 0;
  /* ----------------------- End of Subroutine DPJIBT ---------------------- */
} /* dpjibt_ */

/* DECK DSLSBT */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dslsbt_
   (doublereal *wm, integer *iwm, doublereal *x, doublereal *tem)
{
  static integer mb, nb, lpb, lpc, lblox;

  /* ----------------------------------------------------------------------- */
  /* This routine acts as an interface between the core integrator */
  /* routine and the DSOLBT routine for the solution of the linear system */
  /* arising from chord iteration. */
  /* Communication with DSLSBT uses the following variables: */
  /* WM    = real work space containing the LU decomposition, */
  /*         starting at WM(3). */
  /* IWM   = integer work space containing pivot information, starting at */
  /*         IWM(21).  IWM also contains block structure parameters */
  /*         MB = IWM(1) and NB = IWM(2). */
  /* X     = the right-hand side vector on input, and the solution vector */
  /*         on output, of length N. */
  /* TEM   = vector of work space of length N, not used in this version. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --tem;
  --x;
  --iwm;
  --wm;

  /* Function Body */
  mb = iwm[1];
  nb = iwm[2];
  lblox = mb * mb * nb;
  lpb = lblox + 3;
  lpc = lpb + lblox;
  dsolbt_(&mb, &nb, &wm[3], &wm[lpb], &wm[lpc], &x[1], &iwm[21]);
  return 0;
  /* ----------------------- End of Subroutine DSLSBT ---------------------- */
} /* dslsbt_ */

/* DECK DDECBT */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::ddecbt_
   (integer *m, integer *n, doublereal *a, doublereal *b, doublereal *c__,
   integer *ip, integer *ier)
{
  /* System generated locals */
  integer ip_dim1, ip_offset, a_dim1, a_dim2, a_offset, b_dim1, b_dim2,
    b_offset, c_dim1, c_dim2, c_offset, i__1, i__2, i__3;

  /* Local variables */
  static integer i__, j, k;
  static doublereal dp;
  static integer km1, nm1, nm2;

  /* ----------------------------------------------------------------------- */
  /* Block-tridiagonal matrix decomposition routine. */
  /* Written by A. C. Hindmarsh. */
  /* Latest revision:  November 10, 1983 (ACH) */
  /* Reference:  UCID-30150 */
  /*             Solution of Block-Tridiagonal Systems of Linear */
  /*             Algebraic Equations */
  /*             A.C. Hindmarsh */
  /*             February 1977 */
  /* The input matrix contains three blocks of elements in each block-row, */
  /* including blocks in the (1,3) and (N,N-2) block positions. */
  /* DDECBT uses block Gauss elimination and Subroutines DGEFA and DGESL */
  /* for solution of blocks.  Partial pivoting is done within */
  /* block-rows only. */

  /* Note: this version uses LINPACK routines DGEFA/DGESL instead of */
  /* of dec/sol for solution of blocks, and it uses the BLAS routine DDOT */
  /* for dot product calculations. */

  /* Input: */
  /*     M = order of each block. */
  /*     N = number of blocks in each direction of the matrix. */
  /*         N must be 4 or more.  The complete matrix has order M*N. */
  /*     A = M by M by N array containing diagonal blocks. */
  /*         A(i,j,k) contains the (i,j) element of the k-th block. */
  /*     B = M by M by N array containing the super-diagonal blocks */
  /*         (in B(*,*,k) for k = 1,...,N-1) and the block in the (N,N-2) */
  /*         block position (in B(*,*,N)). */
  /*     C = M by M by N array containing the subdiagonal blocks */
  /*         (in C(*,*,k) for k = 2,3,...,N) and the block in the */
  /*         (1,3) block position (in C(*,*,1)). */
  /*    IP = integer array of length M*N for working storage. */
  /* Output: */
  /* A,B,C = M by M by N arrays containing the block-LU decomposition */
  /*         of the input matrix. */
  /*    IP = M by N array of pivot information.  IP(*,k) contains */
  /*         information for the k-th digonal block. */
  /*   IER = 0  if no trouble occurred, or */
  /*       = -1 if the input value of M or N was illegal, or */
  /*       = k  if a singular matrix was found in the k-th diagonal block. */
  /* Use DSOLBT to solve the associated linear system. */

  /* External routines required: DGEFA and DGESL (from LINPACK) and */
  /* DDOT (from the BLAS, or Basic Linear Algebra package). */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  ip_dim1 =  *m;
  ip_offset = 1+ip_dim1;
  ip -= ip_offset;
  c_dim1 =  *m;
  c_dim2 =  *m;
  c_offset = 1+c_dim1 *(1+c_dim2);
  c__ -= c_offset;
  b_dim1 =  *m;
  b_dim2 =  *m;
  b_offset = 1+b_dim1 *(1+b_dim2);
  b -= b_offset;
  a_dim1 =  *m;
  a_dim2 =  *m;
  a_offset = 1+a_dim1 *(1+a_dim2);
  a -= a_offset;

  /* Function Body */
  if (*m < 1 ||  *n < 4)
  {
    goto L210;
  }
  nm1 =  *n - 1;
  nm2 =  *n - 2;
  /* Process the first block-row. ----------------------------------------- */
  dgefa_(&a[a_offset], m, m, &ip[ip_offset], ier);
  k = 1;
  if (*ier != 0)
  {
    goto L200;
  }
  i__1 =  *m;
  for (j = 1; j <= i__1; ++j)
  {
    dgesl_(&a[a_offset], m, m, &ip[ip_offset], &b[(j + b_dim2) *b_dim1 + 1],
      &c__0);
    dgesl_(&a[a_offset], m, m, &ip[ip_offset], &c__[(j + c_dim2) *c_dim1 + 1],
      &c__0);
    /* L10: */
  }
  /* Adjust B(*,*,2). ----------------------------------------------------- */
  i__1 =  *m;
  for (j = 1; j <= i__1; ++j)
  {
    i__2 =  *m;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      dp = ddot_(m, &c__[i__ + ((c_dim2 << 1) + 1) *c_dim1], m, &c__[(j +
        c_dim2) *c_dim1 + 1], &c__1);
      b[i__ + (j + (b_dim2 << 1)) *b_dim1] -= dp;
      /* L30: */
    }
    /* L40: */
  }
  /* Main loop.  Process block-rows 2 to N-1. ----------------------------- */
  i__1 = nm1;
  for (k = 2; k <= i__1; ++k)
  {
    km1 = k - 1;
    i__2 =  *m;
    for (j = 1; j <= i__2; ++j)
    {
      i__3 =  *m;
      for (i__ = 1; i__ <= i__3; ++i__)
      {
        dp = ddot_(m, &c__[i__ + (k *c_dim2 + 1) *c_dim1], m, &b[(j + km1 *
          b_dim2) *b_dim1 + 1], &c__1);
        a[i__ + (j + k * a_dim2) *a_dim1] -= dp;
        /* L60: */
      }
      /* L70: */
    }
    dgefa_(&a[(k *a_dim2 + 1) *a_dim1 + 1], m, m, &ip[k *ip_dim1 + 1], ier);
    if (*ier != 0)
    {
      goto L200;
    }
    i__2 =  *m;
    for (j = 1; j <= i__2; ++j)
    {
      /* L80: */
      dgesl_(&a[(k *a_dim2 + 1) *a_dim1 + 1], m, m, &ip[k *ip_dim1 + 1], &b[(j 
        + k * b_dim2) *b_dim1 + 1], &c__0);
    }
    /* L100: */
  }
  /* Process last block-row and return. ----------------------------------- */
  i__1 =  *m;
  for (j = 1; j <= i__1; ++j)
  {
    i__2 =  *m;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      dp = ddot_(m, &b[i__ + (*n * b_dim2 + 1) *b_dim1], m, &b[(j + nm2 *b_dim2)
        *b_dim1 + 1], &c__1);
      c__[i__ + (j +  *n * c_dim2) *c_dim1] -= dp;
      /* L120: */
    }
    /* L130: */
  }
  i__1 =  *m;
  for (j = 1; j <= i__1; ++j)
  {
    i__2 =  *m;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      dp = ddot_(m, &c__[i__ + (*n * c_dim2 + 1) *c_dim1], m, &b[(j + nm1
        *b_dim2) *b_dim1 + 1], &c__1);
      a[i__ + (j +  *n * a_dim2) *a_dim1] -= dp;
      /* L150: */
    }
    /* L160: */
  }
  dgefa_(&a[(*n * a_dim2 + 1) *a_dim1 + 1], m, m, &ip[ *n * ip_dim1 + 1], ier);
  k =  *n;
  if (*ier != 0)
  {
    goto L200;
  }
  return 0;
  /* Error returns. ------------------------------------------------------- */
  L200: *ier = k;
  return 0;
  L210: *ier =  - 1;
  return 0;
  /* ----------------------- End of Subroutine DDECBT ---------------------- */
} /* ddecbt_ */

/* DECK DSOLBT */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dsolbt_
   (integer *m, integer *n, doublereal *a, doublereal *b, doublereal *c__,
   doublereal *y, integer *ip)
{
  /* System generated locals */
  integer ip_dim1, ip_offset, a_dim1, a_dim2, a_offset, b_dim1, b_dim2,
    b_offset, c_dim1, c_dim2, c_offset, y_dim1, y_offset, i__1, i__2;

  /* Local variables */
  static integer i__, k, kb;
  static doublereal dp;
  static integer km1, nm1, nm2, kp1;

  /* ----------------------------------------------------------------------- */
  /* Solution of block-tridiagonal linear system. */
  /* Coefficient matrix must have been previously processed by DDECBT. */
  /* M, N, A,B,C, and IP  must not have been changed since call to DDECBT. */
  /* Written by A. C. Hindmarsh. */
  /* Input: */
  /*     M = order of each block. */
  /*     N = number of blocks in each direction of matrix. */
  /* A,B,C = M by M by N arrays containing block LU decomposition */
  /*         of coefficient matrix from DDECBT. */
  /*    IP = M by N integer array of pivot information from DDECBT. */
  /*     Y = array of length M*N containg the right-hand side vector */
  /*         (treated as an M by N array here). */
  /* Output: */
  /*     Y = solution vector, of length M*N. */

  /* External routines required: DGESL (LINPACK) and DDOT (BLAS). */
  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  ip_dim1 =  *m;
  ip_offset = 1+ip_dim1;
  ip -= ip_offset;
  y_dim1 =  *m;
  y_offset = 1+y_dim1;
  y -= y_offset;
  c_dim1 =  *m;
  c_dim2 =  *m;
  c_offset = 1+c_dim1 *(1+c_dim2);
  c__ -= c_offset;
  b_dim1 =  *m;
  b_dim2 =  *m;
  b_offset = 1+b_dim1 *(1+b_dim2);
  b -= b_offset;
  a_dim1 =  *m;
  a_dim2 =  *m;
  a_offset = 1+a_dim1 *(1+a_dim2);
  a -= a_offset;

  /* Function Body */
  nm1 =  *n - 1;
  nm2 =  *n - 2;
  /* Forward solution sweep. ---------------------------------------------- */
  dgesl_(&a[a_offset], m, m, &ip[ip_offset], &y[y_offset], &c__0);
  i__1 = nm1;
  for (k = 2; k <= i__1; ++k)
  {
    km1 = k - 1;
    i__2 =  *m;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      dp = ddot_(m, &c__[i__ + (k *c_dim2 + 1) *c_dim1], m, &y[km1 *y_dim1 + 1],
        &c__1);
      y[i__ + k * y_dim1] -= dp;
      /* L20: */
    }
    dgesl_(&a[(k *a_dim2 + 1) *a_dim1 + 1], m, m, &ip[k *ip_dim1 + 1],  &y[k
      *y_dim1 + 1], &c__0);
    /* L30: */
  }
  i__1 =  *m;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    dp = ddot_(m, &c__[i__ + (*n * c_dim2 + 1) *c_dim1], m, &y[nm1 *y_dim1 + 1],
      &c__1) + ddot_(m, &b[i__ + (*n * b_dim2 + 1) *b_dim1], m, &y[nm2 *y_dim1 
      + 1], &c__1);
    y[i__ +  *n * y_dim1] -= dp;
    /* L50: */
  }
  dgesl_(&a[(*n * a_dim2 + 1) *a_dim1 + 1], m, m, &ip[ *n * ip_dim1 + 1], &y[
    *n * y_dim1 + 1], &c__0);
  /* Backward solution sweep. --------------------------------------------- */
  i__1 = nm1;
  for (kb = 1; kb <= i__1; ++kb)
  {
    k =  *n - kb;
    kp1 = k + 1;
    i__2 =  *m;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      dp = ddot_(m, &b[i__ + (k *b_dim2 + 1) *b_dim1], m, &y[kp1 *y_dim1 + 1],
        &c__1);
      y[i__ + k * y_dim1] -= dp;
      /* L70: */
    }
    /* L80: */
  }
  i__1 =  *m;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    dp = ddot_(m, &c__[i__ + (c_dim2 + 1) *c_dim1], m, &y[y_dim1 *3+1], &c__1);
    y[i__ + y_dim1] -= dp;
    /* L100: */
  }
  return 0;
  /* ----------------------- End of Subroutine DSOLBT ---------------------- */
} /* dsolbt_ */

/* DECK DIPREPI */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider
   ::diprepi_(integer *neq, doublereal *y, doublereal *s, doublereal *rwork,
   integer *ia, integer *ja, integer *ic, integer *jc, integer *ipflag, U_fp
   res, U_fp jac, U_fp adda)
{
  /* System generated locals */
  integer i__1;

  /* Local variables */
  static integer i__, imax, lyhd, lyhn, lewtn;

  /* ----------------------------------------------------------------------- */
  /* This routine serves as an interface between the driver and */
  /* Subroutine DPREPI.  Tasks performed here are: */
  /*  * call DPREPI, */
  /*  * reset the required WM segment length LENWK, */
  /*  * move YH back to its final location (following WM in RWORK), */
  /*  * reset pointers for YH, SAVR, EWT, and ACOR, and */
  /*  * move EWT to its new position if ISTATE = 0 or 1. */
  /* IPFLAG is an output error indication flag.  IPFLAG = 0 if there was */
  /* no trouble, and IPFLAG is the value of the DPREPI error flag IPPER */
  /* if there was trouble in Subroutine DPREPI. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --jc;
  --ic;
  --ja;
  --ia;
  --rwork;
  --s;
  --y;
  --neq;

  /* Function Body */
  *ipflag = 0;
  /* Call DPREPI to do matrix preprocessing operations. ------------------- */
  dprepi_(&neq[1], &y[1], &s[1], &rwork[dls001_1.lyh], &rwork[dls001_1.lsavf],
    &rwork[dls001_1.lewt], &rwork[dls001_1.lacor], &ia[1], &ja[1], &ic[1],
    &jc[1], &rwork[dls001_1.lwm], (integer*) &rwork[dls001_1.lwm], ipflag, 
    (U_fp)res, (U_fp)jac, (U_fp)adda);
  dlss01_1.lenwk = max(dlss01_1.lreq, dlss01_1.lwmin);
  if (*ipflag < 0)
  {
    return 0;
  }
  /* If DPREPI was successful, move YH to end of required space for WM. --- */
  lyhn = dls001_1.lwm + dlss01_1.lenwk;
  if (lyhn > dls001_1.lyh)
  {
    return 0;
  }
  lyhd = dls001_1.lyh - lyhn;
  if (lyhd == 0)
  {
    goto L20;
  }
  imax = lyhn - 1+dlss01_1.lenyhm;
  i__1 = imax;
  for (i__ = lyhn; i__ <= i__1; ++i__)
  {
    /* L10: */
    rwork[i__] = rwork[i__ + lyhd];
  }
  dls001_1.lyh = lyhn;
  /* Reset pointers for SAVR, EWT, and ACOR. ------------------------------ */
  L20: dls001_1.lsavf = dls001_1.lyh + dlss01_1.lenyh;
  lewtn = dls001_1.lsavf + dls001_1.n;
  dls001_1.lacor = lewtn + dls001_1.n;
  if (dlss01_1.istatc == 3)
  {
    goto L40;
  }
  /* If ISTATE = 1, move EWT (left) to its new position. ------------------ */
  if (lewtn > dls001_1.lewt)
  {
    return 0;
  }
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L30: */
    rwork[i__ + lewtn - 1] = rwork[i__ + dls001_1.lewt - 1];
  }
  L40: dls001_1.lewt = lewtn;
  return 0;
  /* ----------------------- End of Subroutine DIPREPI --------------------- */
} /* diprepi_ */

/* DECK DPREPI */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dprepi_
   (integer *neq, doublereal *y, doublereal *s, doublereal *yh, doublereal
   *savr, doublereal *ewt, doublereal *rtem, integer *ia, integer *ja, integer
   *ic, integer *jc, doublereal *wk, integer *iwk, integer *ipper, S_fp res,
   S_fp jac, S_fp adda)
{
  /* System generated locals */
  integer i__1, i__2;

  /* Local variables */
  static integer i__, j, k;
  static doublereal yj;
  static integer np1;
  static doublereal fac;
  static integer ibr, ier, ldif, ljfo, ipil, maxg, ipiu, knew, liwk;
  static doublereal erwt;
  static integer iptt1, iptt2;
  static integer kamin, kamax, kcmax, kcmin, nzsut, lenwk1, lenigp;

  /* ----------------------------------------------------------------------- */
  /* This routine performs preprocessing related to the sparse linear */
  /* systems that must be solved. */
  /* The operations that are performed here are: */
  /*  * compute sparseness structure of the iteration matrix */
  /*      P = A - con*J  according to MOSS, */
  /*  * compute grouping of column indices (MITER = 2), */
  /*  * compute a new ordering of rows and columns of the matrix, */
  /*  * reorder JA corresponding to the new ordering, */
  /*  * perform a symbolic LU factorization of the matrix, and */
  /*  * set pointers for segments of the IWK/WK array. */
  /* In addition to variables described previously, DPREPI uses the */
  /* following for communication: */
  /* YH     = the history array.  Only the first column, containing the */
  /*          current Y vector, is used.  Used only if MOSS .ne. 0. */
  /* S      = array of length NEQ, identical to YDOTI in the driver, used */
  /*          only if MOSS .ne. 0. */
  /* SAVR   = a work array of length NEQ, used only if MOSS .ne. 0. */
  /* EWT    = array of length NEQ containing (inverted) error weights. */
  /*          Used only if MOSS = 2 or 4 or if ISTATE = MOSS = 1. */
  /* RTEM   = a work array of length NEQ, identical to ACOR in the driver, */
  /*          used only if MOSS = 2 or 4. */
  /* WK     = a real work array of length LENWK, identical to WM in */
  /*          the driver. */
  /* IWK    = integer work array, assumed to occupy the same space as WK. */
  /* LENWK  = the length of the work arrays WK and IWK. */
  /* ISTATC = a copy of the driver input argument ISTATE (= 1 on the */
  /*          first call, = 3 on a continuation call). */
  /* IYS    = flag value from ODRV or CDRV. */
  /* IPPER  = output error flag , with the following values and meanings: */
  /*        =   0  no error. */
  /*        =  -1  insufficient storage for internal structure pointers. */
  /*        =  -2  insufficient storage for JGROUP. */
  /*        =  -3  insufficient storage for ODRV. */
  /*        =  -4  other error flag from ODRV (should never occur). */
  /*        =  -5  insufficient storage for CDRV. */
  /*        =  -6  other error flag from CDRV. */
  /*        =  -7  if the RES routine returned error flag IRES = IER = 2. */
  /*        =  -8  if the RES routine returned error flag IRES = IER = 3. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --iwk;
  --wk;
  --jc;
  --ic;
  --ja;
  --ia;
  --rtem;
  --ewt;
  --savr;
  --yh;
  --s;
  --y;
  --neq;

  /* Function Body */
  dlss01_1.ibian = dlss01_1.lrat << 1;
  dlss01_1.ipian = dlss01_1.ibian + 1;
  np1 = dls001_1.n + 1;
  dlss01_1.ipjan = dlss01_1.ipian + np1;
  dlss01_1.ibjan = dlss01_1.ipjan - 1;
  lenwk1 = dlss01_1.lenwk - dls001_1.n;
  liwk = dlss01_1.lenwk * dlss01_1.lrat;
  if (dlss01_1.moss == 0)
  {
    liwk -= dls001_1.n;
  }
  if (dlss01_1.moss == 1 || dlss01_1.moss == 2)
  {
    liwk = lenwk1 * dlss01_1.lrat;
  }
  if (dlss01_1.ipjan + dls001_1.n - 1 > liwk)
  {
    goto L310;
  }
  if (dlss01_1.moss == 0)
  {
    goto L30;
  }

  if (dlss01_1.istatc == 3)
  {
    goto L20;
  }
  /* ISTATE = 1 and MOSS .ne. 0.  Perturb Y for structure determination. */
  /* Initialize S with random nonzero elements for structure determination. */
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    erwt = 1. / ewt[i__];
    fac = 1. / (i__ + 1.) + 1.;
    y[i__] += fac * d_sign(&erwt, &y[i__]);
    s[i__] = fac * erwt + 1.;
    /* L10: */
  }
  switch (dlss01_1.moss)
  {
    case 1:
      goto L70;
    case 2:
      goto L100;
    case 3:
      goto L150;
    case 4:
      goto L200;
  }

  L20: 
  /* ISTATE = 3 and MOSS .ne. 0. Load Y from YH(*,1) and S from YH(*,2). -- */
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    y[i__] = yh[i__];
    /* L25: */
    s[i__] = yh[dls001_1.n + i__];
  }
  switch (dlss01_1.moss)
  {
    case 1:
      goto L70;
    case 2:
      goto L100;
    case 3:
      goto L150;
    case 4:
      goto L200;
  }

  /* MOSS = 0. Process user's IA,JA and IC,JC. ---------------------------- */
  L30: knew = dlss01_1.ipjan;
  kamin = ia[1];
  kcmin = ic[1];
  iwk[dlss01_1.ipian] = 1;
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L35: */
      iwk[liwk + i__] = 0;
    }
    kamax = ia[j + 1] - 1;
    if (kamin > kamax)
    {
      goto L45;
    }
    i__2 = kamax;
    for (k = kamin; k <= i__2; ++k)
    {
      i__ = ja[k];
      iwk[liwk + i__] = 1;
      if (knew > liwk)
      {
        goto L310;
      }
      iwk[knew] = i__;
      ++knew;
      /* L40: */
    }
    L45: kamin = kamax + 1;
    kcmax = ic[j + 1] - 1;
    if (kcmin > kcmax)
    {
      goto L55;
    }
    i__2 = kcmax;
    for (k = kcmin; k <= i__2; ++k)
    {
      i__ = jc[k];
      if (iwk[liwk + i__] != 0)
      {
        goto L50;
      }
      if (knew > liwk)
      {
        goto L310;
      }
      iwk[knew] = i__;
      ++knew;
      L50: ;
    }
    L55: iwk[dlss01_1.ipian + j] = knew + 1-dlss01_1.ipjan;
    kcmin = kcmax + 1;
    /* L60: */
  }
  goto L240;

  /* MOSS = 1. Compute structure from user-supplied Jacobian routine JAC. - */
  L70: 
  /* A dummy call to RES allows user to create temporaries for use in JAC. */
  ier = 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ier);
  if (ier > 1)
  {
    goto L370;
  }
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    savr[i__] = 0.;
    /* L75: */
    wk[lenwk1 + i__] = 0.;
  }
  k = dlss01_1.ipjan;
  iwk[dlss01_1.ipian] = 1;
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    (*adda)(&neq[1], &dls001_1.tn, &y[1], &j, &iwk[dlss01_1.ipian],
      &iwk[dlss01_1.ipjan], &wk[lenwk1 + 1]);
    (*jac)(&neq[1], &dls001_1.tn, &y[1], &s[1], &j, &iwk[dlss01_1.ipian], 
      &iwk[dlss01_1.ipjan], &savr[1]);
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      ljfo = lenwk1 + i__;
      if (wk[ljfo] == 0.)
      {
        goto L80;
      }
      wk[ljfo] = 0.;
      savr[i__] = 0.;
      goto L85;
      L80: if (savr[i__] == 0.)
      {
        goto L90;
      }
      savr[i__] = 0.;
      L85: if (k > liwk)
      {
        goto L310;
      }
      iwk[k] = i__;
      ++k;
      L90: ;
    }
    iwk[dlss01_1.ipian + j] = k + 1-dlss01_1.ipjan;
    /* L95: */
  }
  goto L240;

  /* MOSS = 2. Compute structure from results of N + 1 calls to RES. ------ */
  L100: i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L105: */
    wk[lenwk1 + i__] = 0.;
  }
  k = dlss01_1.ipjan;
  iwk[dlss01_1.ipian] = 1;
  ier =  - 1;
  if (dls001_1.miter == 1)
  {
    ier = 1;
  }
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ier);
  if (ier > 1)
  {
    goto L370;
  }
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    (*adda)(&neq[1], &dls001_1.tn, &y[1], &j, &iwk[dlss01_1.ipian],
      &iwk[dlss01_1.ipjan], &wk[lenwk1 + 1]);
    yj = y[j];
    erwt = 1. / ewt[j];
    y[j] = yj + d_sign(&erwt, &yj);
    (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &rtem[1], &ier);
    if (ier > 1)
    {
      return 0;
    }
    y[j] = yj;
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      ljfo = lenwk1 + i__;
      if (wk[ljfo] == 0.)
      {
        goto L110;
      }
      wk[ljfo] = 0.;
      goto L115;
      L110: if (rtem[i__] == savr[i__])
      {
        goto L120;
      }
      L115: if (k > liwk)
      {
        goto L310;
      }
      iwk[k] = i__;
      ++k;
      L120: ;
    }
    iwk[dlss01_1.ipian + j] = k + 1-dlss01_1.ipjan;
    /* L130: */
  }
  goto L240;

  /* MOSS = 3. Compute structure from the user's IA/JA and JAC routine. --- */
  L150: 
  /* A dummy call to RES allows user to create temporaries for use in JAC. */
  ier = 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ier);
  if (ier > 1)
  {
    goto L370;
  }
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L155: */
    savr[i__] = 0.;
  }
  knew = dlss01_1.ipjan;
  kamin = ia[1];
  iwk[dlss01_1.ipian] = 1;
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    (*jac)(&neq[1], &dls001_1.tn, &y[1], &s[1], &j, &iwk[dlss01_1.ipian], 
      &iwk[dlss01_1.ipjan], &savr[1]);
    kamax = ia[j + 1] - 1;
    if (kamin > kamax)
    {
      goto L170;
    }
    i__2 = kamax;
    for (k = kamin; k <= i__2; ++k)
    {
      i__ = ja[k];
      savr[i__] = 0.;
      if (knew > liwk)
      {
        goto L310;
      }
      iwk[knew] = i__;
      ++knew;
      /* L160: */
    }
    L170: kamin = kamax + 1;
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      if (savr[i__] == 0.)
      {
        goto L180;
      }
      savr[i__] = 0.;
      if (knew > liwk)
      {
        goto L310;
      }
      iwk[knew] = i__;
      ++knew;
      L180: ;
    }
    iwk[dlss01_1.ipian + j] = knew + 1-dlss01_1.ipjan;
    /* L190: */
  }
  goto L240;

  /* MOSS = 4. Compute structure from user's IA/JA and N + 1 RES calls. --- */
  L200: knew = dlss01_1.ipjan;
  kamin = ia[1];
  iwk[dlss01_1.ipian] = 1;
  ier =  - 1;
  if (dls001_1.miter == 1)
  {
    ier = 1;
  }
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ier);
  if (ier > 1)
  {
    goto L370;
  }
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    yj = y[j];
    erwt = 1. / ewt[j];
    y[j] = yj + d_sign(&erwt, &yj);
    (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &rtem[1], &ier);
    if (ier > 1)
    {
      return 0;
    }
    y[j] = yj;
    kamax = ia[j + 1] - 1;
    if (kamin > kamax)
    {
      goto L225;
    }
    i__2 = kamax;
    for (k = kamin; k <= i__2; ++k)
    {
      i__ = ja[k];
      rtem[i__] = savr[i__];
      if (knew > liwk)
      {
        goto L310;
      }
      iwk[knew] = i__;
      ++knew;
      /* L220: */
    }
    L225: kamin = kamax + 1;
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      if (rtem[i__] == savr[i__])
      {
        goto L230;
      }
      if (knew > liwk)
      {
        goto L310;
      }
      iwk[knew] = i__;
      ++knew;
      L230: ;
    }
    iwk[dlss01_1.ipian + j] = knew + 1-dlss01_1.ipjan;
    /* L235: */
  }

  L240: if (dlss01_1.moss == 0 || dlss01_1.istatc == 3)
  {
    goto L250;
  }
  /* If ISTATE = 0 or 1 and MOSS .ne. 0, restore Y from YH. --------------- */
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L245: */
    y[i__] = yh[i__];
  }
  L250: dlss01_1.nnz = iwk[dlss01_1.ipian + dls001_1.n] - 1;
  *ipper = 0;
  dlss01_1.ngp = 0;
  lenigp = 0;
  dlss01_1.ipigp = dlss01_1.ipjan + dlss01_1.nnz;
  if (dls001_1.miter != 2)
  {
    goto L260;
  }

  /* Compute grouping of column indices (MITER = 2). ---------------------- */

  maxg = np1;
  dlss01_1.ipjgp = dlss01_1.ipjan + dlss01_1.nnz;
  dlss01_1.ibjgp = dlss01_1.ipjgp - 1;
  dlss01_1.ipigp = dlss01_1.ipjgp + dls001_1.n;
  iptt1 = dlss01_1.ipigp + np1;
  iptt2 = iptt1 + dls001_1.n;
  dlss01_1.lreq = iptt2 + dls001_1.n - 1;
  if (dlss01_1.lreq > liwk)
  {
    goto L320;
  }
  jgroup_(&dls001_1.n, &iwk[dlss01_1.ipian], &iwk[dlss01_1.ipjan], &maxg,
    &dlss01_1.ngp, &iwk[dlss01_1.ipigp], &iwk[dlss01_1.ipjgp], &iwk[iptt1],
    &iwk[iptt2], &ier);
  if (ier != 0)
  {
    goto L320;
  }
  lenigp = dlss01_1.ngp + 1;

  /* Compute new ordering of rows/columns of Jacobian. -------------------- */
  L260: dlss01_1.ipr = dlss01_1.ipigp + lenigp;
  dlss01_1.ipc = dlss01_1.ipr;
  dlss01_1.ipic = dlss01_1.ipc + dls001_1.n;
  dlss01_1.ipisp = dlss01_1.ipic + dls001_1.n;
  dlss01_1.iprsp = (dlss01_1.ipisp - 2) / dlss01_1.lrat + 2;
  dlss01_1.iesp = dlss01_1.lenwk + 1-dlss01_1.iprsp;
  if (dlss01_1.iesp < 0)
  {
    goto L330;
  }
  ibr = dlss01_1.ipr - 1;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L270: */
    iwk[ibr + i__] = i__;
  }
  dlss01_1.nsp = liwk + 1-dlss01_1.ipisp;
  odrv_(&dls001_1.n, &iwk[dlss01_1.ipian], &iwk[dlss01_1.ipjan], &wk[1],
    &iwk[dlss01_1.ipr], &iwk[dlss01_1.ipic], &dlss01_1.nsp,
    &iwk[dlss01_1.ipisp], &c__1, &dlss01_1.iys);
  if (dlss01_1.iys == dls001_1.n *11+1)
  {
    goto L340;
  }
  if (dlss01_1.iys != 0)
  {
    goto L330;
  }

  /* Reorder JAN and do symbolic LU factorization of matrix. -------------- */
  dlss01_1.ipa = dlss01_1.lenwk + 1-dlss01_1.nnz;
  dlss01_1.nsp = dlss01_1.ipa - dlss01_1.iprsp;
  /* Computing MAX */
  i__1 = dls001_1.n * 12 / dlss01_1.lrat, i__2 = dls001_1.n * 6 / dlss01_1.lrat
    + (dls001_1.n << 1) + dlss01_1.nnz;
  dlss01_1.lreq = max(i__1, i__2) + 3;
  dlss01_1.lreq = dlss01_1.lreq + dlss01_1.iprsp - 1+dlss01_1.nnz;
  if (dlss01_1.lreq > dlss01_1.lenwk)
  {
    goto L350;
  }
  dlss01_1.iba = dlss01_1.ipa - 1;
  i__1 = dlss01_1.nnz;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L280: */
    wk[dlss01_1.iba + i__] = 0.;
  }
  dlss01_1.ipisp = dlss01_1.lrat *(dlss01_1.iprsp - 1) + 1;
  cdrv_(&dls001_1.n, &iwk[dlss01_1.ipr], &iwk[dlss01_1.ipc],
    &iwk[dlss01_1.ipic], &iwk[dlss01_1.ipian], &iwk[dlss01_1.ipjan],
    &wk[dlss01_1.ipa], &wk[dlss01_1.ipa], &wk[dlss01_1.ipa], &dlss01_1.nsp,
    &iwk[dlss01_1.ipisp], &wk[dlss01_1.iprsp], &dlss01_1.iesp, &c__5,
    &dlss01_1.iys);
  dlss01_1.lreq = dlss01_1.lenwk - dlss01_1.iesp;
  if (dlss01_1.iys == dls001_1.n *10+1)
  {
    goto L350;
  }
  if (dlss01_1.iys != 0)
  {
    goto L360;
  }
  ipil = dlss01_1.ipisp;
  ipiu = ipil + (dls001_1.n << 1) + 1;
  dlss01_1.nzu = iwk[ipil + dls001_1.n] - iwk[ipil];
  dlss01_1.nzl = iwk[ipiu + dls001_1.n] - iwk[ipiu];
  if (dlss01_1.lrat > 1)
  {
    goto L290;
  }
  adjlr_(&dls001_1.n, &iwk[dlss01_1.ipisp], &ldif);
  dlss01_1.lreq += ldif;
  L290: if (dlss01_1.lrat == 2 && dlss01_1.nnz == dls001_1.n)
  {
    ++dlss01_1.lreq;
  }
  dlss01_1.nsp = dlss01_1.nsp + dlss01_1.lreq - dlss01_1.lenwk;
  dlss01_1.ipa = dlss01_1.lreq + 1-dlss01_1.nnz;
  dlss01_1.iba = dlss01_1.ipa - 1;
  *ipper = 0;
  return 0;

  L310: *ipper =  - 1;
  dlss01_1.lreq = ((dls001_1.n << 1) + 1) / dlss01_1.lrat + 2;
  /* Computing MAX */
  i__1 = dlss01_1.lenwk + 1;
  dlss01_1.lreq = max(i__1, dlss01_1.lreq);
  return 0;

  L320: *ipper =  - 2;
  dlss01_1.lreq = (dlss01_1.lreq - 1) / dlss01_1.lrat + 1;
  return 0;

  L330: *ipper =  - 3;
  cntnzu_(&dls001_1.n, &iwk[dlss01_1.ipian], &iwk[dlss01_1.ipjan], &nzsut);
  dlss01_1.lreq = dlss01_1.lenwk - dlss01_1.iesp + (dls001_1.n *3+(nzsut << 2) 
    - 1) / dlss01_1.lrat + 1;
  return 0;

  L340: *ipper =  - 4;
  return 0;

  L350: *ipper =  - 5;
  return 0;

  L360: *ipper =  - 6;
  dlss01_1.lreq = dlss01_1.lenwk;
  return 0;

  L370: *ipper =  - ier - 5;
  dlss01_1.lreq = ((dls001_1.n << 1) + 1) / dlss01_1.lrat + 2;
  return 0;
  /* ----------------------- End of Subroutine DPREPI ---------------------- */
} /* dprepi_ */

/* DECK DAINVGS */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider
   ::dainvgs_(integer *neq, doublereal *t, doublereal *y, doublereal *wk,
   integer *iwk, doublereal *tem, doublereal *ydot, integer *ier, S_fp res,
   S_fp adda)
{
  /* System generated locals */
  integer i__1, i__2;

  /* Local variables */
  static integer i__, j, k, kmin;
  static integer kmax, imul;

  /* ----------------------------------------------------------------------- */
  /* This subroutine computes the initial value of the vector YDOT */
  /* satisfying */
  /*     A * YDOT = g(t,y) */
  /* when A is nonsingular.  It is called by DLSODIS for initialization */
  /* only, when ISTATE = 0.  The matrix A is subjected to LU */
  /* decomposition in CDRV.  Then the system A*YDOT = g(t,y) is solved */
  /* in CDRV. */
  /* In addition to variables described previously, communication */
  /* with DAINVGS uses the following: */
  /* Y     = array of initial values. */
  /* WK    = real work space for matrices.  On output it contains A and */
  /*         its LU decomposition.  The LU decomposition is not entirely */
  /*         sparse unless the structure of the matrix A is identical to */
  /*         the structure of the Jacobian matrix dr/dy. */
  /*         Storage of matrix elements starts at WK(3). */
  /*         WK(1) = SQRT(UROUND), not used here. */
  /* IWK   = integer work space for matrix-related data, assumed to */
  /*         be equivalenced to WK.  In addition, WK(IPRSP) and WK(IPISP) */
  /*         are assumed to have identical locations. */
  /* TEM   = vector of work space of length N (ACOR in DSTODI). */
  /* YDOT  = output vector containing the initial dy/dt. YDOT(i) contains */
  /*         dy(i)/dt when the matrix A is non-singular. */
  /* IER   = output error flag with the following values and meanings: */
  /*       = 0  if DAINVGS was successful. */
  /*       = 1  if the A-matrix was found to be singular. */
  /*       = 2  if RES returned an error flag IRES = IER = 2. */
  /*       = 3  if RES returned an error flag IRES = IER = 3. */
  /*       = 4  if insufficient storage for CDRV (should not occur here). */
  /*       = 5  if other error found in CDRV (should not occur here). */
  /* ----------------------------------------------------------------------- */

  /* Parameter adjustments */
  --ydot;
  --tem;
  --iwk;
  --wk;
  --y;

  /* Function Body */
  i__1 = dlss01_1.nnz;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L10: */
    wk[dlss01_1.iba + i__] = 0.;
  }

  *ier = 1;
  (*res)(neq, t, &y[1], &wk[dlss01_1.ipa], &ydot[1], ier);
  if (*ier > 1)
  {
    return 0;
  }

  kmin = iwk[dlss01_1.ipian];
  i__1 =  *neq;
  for (j = 1; j <= i__1; ++j)
  {
    kmax = iwk[dlss01_1.ipian + j] - 1;
    i__2 = kmax;
    for (k = kmin; k <= i__2; ++k)
    {
      i__ = iwk[dlss01_1.ibjan + k];
      /* L15: */
      tem[i__] = 0.;
    }
    (*adda)(neq, t, &y[1], &j, &iwk[dlss01_1.ipian], &iwk[dlss01_1.ipjan], 
      &tem[1]);
    i__2 = kmax;
    for (k = kmin; k <= i__2; ++k)
    {
      i__ = iwk[dlss01_1.ibjan + k];
      /* L20: */
      wk[dlss01_1.iba + k] = tem[i__];
    }
    kmin = kmax + 1;
    /* L30: */
  }
  ++dlss01_1.nlu;
  *ier = 0;
  i__1 =  *neq;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L40: */
    tem[i__] = 0.;
  }

  /* Numerical factorization of matrix A. --------------------------------- */
  cdrv_(neq, &iwk[dlss01_1.ipr], &iwk[dlss01_1.ipc], &iwk[dlss01_1.ipic],
    &iwk[dlss01_1.ipian], &iwk[dlss01_1.ipjan], &wk[dlss01_1.ipa], &tem[1],
    &tem[1], &dlss01_1.nsp, &iwk[dlss01_1.ipisp], &wk[dlss01_1.iprsp],
    &dlss01_1.iesp, &c__2, &dlss01_1.iys);
  if (dlss01_1.iys == 0)
  {
    goto L50;
  }
  imul = (dlss01_1.iys - 1) /  *neq;
  *ier = 5;
  if (imul == 8)
  {
    *ier = 1;
  }
  if (imul == 10)
  {
    *ier = 4;
  }
  return 0;

  /* Solution of the linear system. --------------------------------------- */
  L50: cdrv_(neq, &iwk[dlss01_1.ipr], &iwk[dlss01_1.ipc], &iwk[dlss01_1.ipic],
    &iwk[dlss01_1.ipian], &iwk[dlss01_1.ipjan], &wk[dlss01_1.ipa], &ydot[1],
    &ydot[1], &dlss01_1.nsp, &iwk[dlss01_1.ipisp], &wk[dlss01_1.iprsp],
    &dlss01_1.iesp, &c__4, &dlss01_1.iys);
  if (dlss01_1.iys != 0)
  {
    *ier = 5;
  }
  return 0;
  /* ----------------------- End of Subroutine DAINVGS --------------------- */
} /* dainvgs_ */

/* DECK DPRJIS */
 /* Subroutine */int SmartMathLibrary::OdePack::ManagedOdePackProvider::dprjis_
   (integer *neq, doublereal *y, doublereal *yh, integer *nyh, doublereal *ewt,
   doublereal *rtem, doublereal *savr, doublereal *s, doublereal *wk, integer
   *iwk, S_fp res, S_fp jac, S_fp adda)
{
  /* System generated locals */
  integer yh_dim1, yh_offset, i__1, i__2, i__3;
  doublereal d__1, d__2, d__3;

  /* Local variables */
  static integer i__, j, k;
  static doublereal r__;
  static integer jj, ng;
  static doublereal hl0, fac, con;
  static integer jmin, jmax, imul, ires, kmax, kmin;
  static doublereal srur;

  /* ----------------------------------------------------------------------- */
  /* DPRJIS is called to compute and process the matrix */
  /* P = A - H*EL(1)*J, where J is an approximation to the Jacobian dr/dy, */
  /* where r = g(t,y) - A(t,y)*s.  J is computed by columns, either by */
  /* the user-supplied routine JAC if MITER = 1, or by finite differencing */
  /* if MITER = 2.  J is stored in WK, rescaled, and ADDA is called to */
  /* generate P.  The matrix P is subjected to LU decomposition in CDRV. */
  /* P and its LU decomposition are stored separately in WK. */

  /* In addition to variables described previously, communication */
  /* with DPRJIS uses the following: */
  /* Y     = array containing predicted values on entry. */
  /* RTEM  = work array of length N (ACOR in DSTODI). */
  /* SAVR  = array containing r evaluated at predicted y. On output it */
  /*         contains the residual evaluated at current values of t and y. */
  /* S     = array containing predicted values of dy/dt (SAVF in DSTODI). */
  /* WK    = real work space for matrices.  On output it contains P and */
  /*         its sparse LU decomposition.  Storage of matrix elements */
  /*         starts at WK(3). */
  /*         WK also contains the following matrix-related data. */
  /*         WK(1) = SQRT(UROUND), used in numerical Jacobian increments. */
  /* IWK   = integer work space for matrix-related data, assumed to be */
  /*         equivalenced to WK.  In addition,  WK(IPRSP) and IWK(IPISP) */
  /*         are assumed to have identical locations. */
  /* EL0   = EL(1) (input). */
  /* IERPJ = output error flag (in COMMON). */
  /*         =  0 if no error. */
  /*         =  1 if zero pivot found in CDRV. */
  /*         = IRES (= 2 or 3) if RES returned IRES = 2 or 3. */
  /*         = -1 if insufficient storage for CDRV (should not occur). */
  /*         = -2 if other error found in CDRV (should not occur here). */
  /* JCUR  = output flag = 1 to indicate that the Jacobian matrix */
  /*         (or approximation) is now current. */
  /* This routine also uses other variables in Common. */
  /* ----------------------------------------------------------------------- */
  /* Parameter adjustments */
  --neq;
  --y;
  yh_dim1 =  *nyh;
  yh_offset = 1+yh_dim1;
  yh -= yh_offset;
  --ewt;
  --rtem;
  --savr;
  --s;
  --wk;
  --iwk;

  /* Function Body */
  hl0 = dls001_1.h__ * dls001_1.el0;
  con =  - hl0;
  dls001_1.jcur = 1;
  ++dls001_1.nje;
  switch (dls001_1.miter)
  {
    case 1:
      goto L100;
    case 2:
      goto L200;
  }

  /* If MITER = 1, call RES, then call JAC and ADDA for each column. ------ */
  L100: ires = 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ires);
  ++dls001_1.nfe;
  if (ires > 1)
  {
    goto L600;
  }
  kmin = iwk[dlss01_1.ipian];
  i__1 = dls001_1.n;
  for (j = 1; j <= i__1; ++j)
  {
    kmax = iwk[dlss01_1.ipian + j] - 1;
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L110: */
      rtem[i__] = 0.;
    }
    (*jac)(&neq[1], &dls001_1.tn, &y[1], &s[1], &j, &iwk[dlss01_1.ipian], 
      &iwk[dlss01_1.ipjan], &rtem[1]);
    i__2 = dls001_1.n;
    for (i__ = 1; i__ <= i__2; ++i__)
    {
      /* L120: */
      rtem[i__] *= con;
    }
    (*adda)(&neq[1], &dls001_1.tn, &y[1], &j, &iwk[dlss01_1.ipian],
      &iwk[dlss01_1.ipjan], &rtem[1]);
    i__2 = kmax;
    for (k = kmin; k <= i__2; ++k)
    {
      i__ = iwk[dlss01_1.ibjan + k];
      wk[dlss01_1.iba + k] = rtem[i__];
      /* L125: */
    }
    kmin = kmax + 1;
    /* L130: */
  }
  goto L290;

  /* If MITER = 2, make NGP + 1 calls to RES to approximate J and P. ------ */
  L200: ires =  - 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ires);
  ++dls001_1.nfe;
  if (ires > 1)
  {
    goto L600;
  }
  srur = wk[1];
  jmin = iwk[dlss01_1.ipigp];
  i__1 = dlss01_1.ngp;
  for (ng = 1; ng <= i__1; ++ng)
  {
    jmax = iwk[dlss01_1.ipigp + ng] - 1;
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      jj = iwk[dlss01_1.ibjgp + j];
      /* Computing MAX */
      d__2 = srur *(d__1 = y[jj], abs(d__1)), d__3 = .01 / ewt[jj];
      r__ = max(d__2, d__3);
      /* L210: */
      y[jj] += r__;
    }
    (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &rtem[1], &ires);
    ++dls001_1.nfe;
    if (ires > 1)
    {
      goto L600;
    }
    i__2 = jmax;
    for (j = jmin; j <= i__2; ++j)
    {
      jj = iwk[dlss01_1.ibjgp + j];
      y[jj] = yh[jj + yh_dim1];
      /* Computing MAX */
      d__2 = srur *(d__1 = y[jj], abs(d__1)), d__3 = .01 / ewt[jj];
      r__ = max(d__2, d__3);
      fac =  - hl0 / r__;
      kmin = iwk[dlss01_1.ibian + jj];
      kmax = iwk[dlss01_1.ibian + jj + 1] - 1;
      i__3 = kmax;
      for (k = kmin; k <= i__3; ++k)
      {
        i__ = iwk[dlss01_1.ibjan + k];
        rtem[i__] = (rtem[i__] - savr[i__]) *fac;
        /* L220: */
      }
      (*adda)(&neq[1], &dls001_1.tn, &y[1], &jj, &iwk[dlss01_1.ipian],
        &iwk[dlss01_1.ipjan], &rtem[1]);
      i__3 = kmax;
      for (k = kmin; k <= i__3; ++k)
      {
        i__ = iwk[dlss01_1.ibjan + k];
        wk[dlss01_1.iba + k] = rtem[i__];
        /* L225: */
      }
      /* L230: */
    }
    jmin = jmax + 1;
    /* L240: */
  }
  ires = 1;
  (*res)(&neq[1], &dls001_1.tn, &y[1], &s[1], &savr[1], &ires);
  ++dls001_1.nfe;
  if (ires > 1)
  {
    goto L600;
  }

  /* Do numerical factorization of P matrix. ------------------------------ */
  L290: ++dlss01_1.nlu;
  dls001_1.ierpj = 0;
  i__1 = dls001_1.n;
  for (i__ = 1; i__ <= i__1; ++i__)
  {
    /* L295: */
    rtem[i__] = 0.;
  }
  cdrv_(&dls001_1.n, &iwk[dlss01_1.ipr], &iwk[dlss01_1.ipc],
    &iwk[dlss01_1.ipic], &iwk[dlss01_1.ipian], &iwk[dlss01_1.ipjan],
    &wk[dlss01_1.ipa], &rtem[1], &rtem[1], &dlss01_1.nsp, &iwk[dlss01_1.ipisp],
    &wk[dlss01_1.iprsp], &dlss01_1.iesp, &c__2, &dlss01_1.iys);
  if (dlss01_1.iys == 0)
  {
    return 0;
  }
  imul = (dlss01_1.iys - 1) / dls001_1.n;
  dls001_1.ierpj =  - 2;
  if (imul == 8)
  {
    dls001_1.ierpj = 1;
  }
  if (imul == 10)
  {
    dls001_1.ierpj =  - 1;
  }
  return 0;
  /* Error return for IRES = 2 or IRES = 3 return from RES. --------------- */
  L600: dls001_1.ierpj = ires;
  return 0;
  /* ----------------------- End of Subroutine DPRJIS ---------------------- */
} /* dprjis_ */
